-3

I want to create a telegram bot that, after seeing the command /define, asks for the word. I want to extract the word sent by the user after the bot asks for it. How do I do it?

import telegram
from telegram.ext import Updater
from telegram.ext import MessageHandler, Filters
from telegram.ext import CommandHandler

updater = Updater(token='******************')
dispatcher = updater.dispatcher

def define(bot, update):
    bot.send_message(chat_id=update.message.chat_id, text="Enter word")
    word = '''get content of following message'''
    definition = get_definition(word)
    bot.send_message(chat_id=update.message.chat_id, text=definiton)

definition_handler = CommandHandler('define', define)

dispatcher.add_handler(definition_handler)

updater.start_polling()
Sharad Bhat
  • 47
  • 2
  • 9
  • Have you tried any code so far? – Sam Chats Jun 30 '17 at 17:14
  • Your question is broad, please read [how to ask](https://stackoverflow.com/help/how-to-ask) – eyllanesc Jun 30 '17 at 17:16
  • @Sharad Bhat I've noticed that you have posted several questions that are missing [a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) . Please review the [Asking questions guidelines](https://stackoverflow.com/help/on-topic) to better your ability to ask questions and receive the answers you need. Also keep in mind that accounts can get prohibited from asking questions if [too many low-quality questions are asked](https://stackoverflow.com/help/quality-standards-error) . Well-asked questions are most likely to attract better answers. Good luck! – Davy M Jun 30 '17 at 17:20

2 Answers2

3
  • First of all, you require pyTelegramBotAPI library;
  • Then, you want to add @BotFather in Telegram and follow the instructure #6. You need to obtain the bot token which is a unique set of letters and digits for your bot, just like a codename. After you have registered a bot via @BotFather, it will give you the token.

Actually, the token is the only thing you need to create any bot you want. The codes for the bot like yours should follow the same logic structure:

# -*- coding: utf-8 -*-
import telebot  # importing pyTelegramBotAPI library
import time
import sys

bot = telebot.Telebot(token='YOUR API TOKEN')  # supply your future bot with the token you have received

@bot.message_handler(commands=['define', 'Define'])
def echo_msg(message):
   echo = bot.send_message(chat_id=message.chat.it,
                           text='What word would you want me to extract, sir?')
   bot.register_next_step_handler(message=echo, callback=extract_msg)

def extract_msg(message):
   msg.append(message.text)
   print(msg)

def main_loop():
   bot.polling(none_stop=True)
   while True:
      time.sleep(1)

if __name__ == '__main__':
   try:
      main_loop()
   except KeyboardInterrupt:
      print(sys.stderr '\nExiting by user request'\n')
      sys.exit(0)

Okay, each bot requires a message_handler to process the incoming information.

In your case, it is a command that triggers the bot to ask for a word to extract into a list. If you do not define bot.register_next_step_handler(), this command will not do any action at all (except the fact it asks for a word).

The function extract_msg() appends the next word written by a user and prints out the msg list into your console.

The function main_loop() runs the bot until suspension and provokes it to idle for a second after each word extraction. To stop the bot, press Ctrl + C.


I hope that suffices. The next step would be to track the person who types /define or /Define and extract his/her word request. Also, it would be better to make msg list more descriptive, or implement absolutely different extraction method. This one is simply informative and hardly applicable in practice.

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
E.Z
  • 1,958
  • 1
  • 18
  • 27
0

I fixed an error in when calling stderr:

# -*- coding: utf-8 -*-
import telebot  # importing pyTelegramBotAPI library
import time
import sys

bot = telebot.Telebot(token='YOUR API TOKEN')  # supply your future bot with the token you have received

@bot.message_handler(commands=['define', 'Define'])
def echo_msg(message):
   echo = bot.send_message(chat_id=message.chat.it,
                           text='What word would you want me to extract, sir?')
   bot.register_next_step_handler(message=echo, callback=extract_msg)

def extract_msg(message):
   msg.append(message.text)
   print(msg)

def main_loop():
   bot.polling(none_stop=True)
   while True:
      time.sleep(1)

if __name__ == '__main__':    try:
      main_loop()    except KeyboardInterrupt:
      print(sys.stderr('\nExiting by user request'\n'))
      sys.exit(0)
Lucas Z
  • 1
  • 1
  • 1