Edwin Cheng
Edwin Cheng

Reputation: 73

Telegram Bot without command line through python

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"?

Upvotes: 1

Views: 2651

Answers (2)

sai ch
sai ch

Reputation: 21

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.

Upvotes: 0

dev4Fun
dev4Fun

Reputation: 1050

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.

Upvotes: 3

Related Questions