1

I am a new beginner in Telegram bot now I test a telegraom bot with simple respond. As we know, we can use python for telegram. There is my code:

from telegram.ext import Updater, CommandHandler
 def hello(bot, update):
     update.message.reply_text(
        'Hello {}'.format(update.message.from_user.first_name))

updater = ipdater(token='xxx') updater.dispatcher.add_handler(CommandHandler('hello', hello)) updater.start_polling() updater.idle()

now i input "/hello" in the telegram bor, it can reply "Hello, myname"

I want to know how to run the bot without commandline?

For example, I input "hello", Can it reply "Hello, myname"?

2 Answers 2

3

Yes, you definitely can do it. You will need to use MessageHandler that reacts to text messages.

Define your callback that reacts based on user's text:

def handle_message(bot, update):
    text = update.message.text
    if text == 'hello':
        update.message.reply_text('Hello {}'.format(update.message.from_user.first_name))

And add message handler to your dispatcher:

dispatcher.add_handler(MessageHandler(filters=Filters.text, callback=handle_message))

Note that message handler takes a filter. In this case it's text, but it can be an image or something else. It's important to know that this will fire for every text message your bot receives. If you gonna be receiving lot of messages I recommend writing more sophisticated filter.

Sign up to request clarification or add additional context in comments.

Comments

0

I echo with @dev4Fun answer - Just only one thing to be noted that bot should have privacy disabled.

To do that -

  1. Remove Bot from Group
  2. ask @botfather to disable privacy
  3. add bot to group again

Than you will recieve all the messages with Filter.Text.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.