Reputation: 73
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
Reputation: 21
I echo with @dev4Fun answer - Just only one thing to be noted that bot should have privacy disabled.
To do that -
- Remove Bot from Group
- ask @botfather to disable privacy
- add bot to group again
Than you will recieve all the messages with Filter.Text.
Upvotes: 0
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