I want to write a telegram bot. I want my bot to send a message when any user start chat in the private chat with my bot.
here is my code
def start_chat(update: Update, context: CallbackContext):
context.bot.send_message(
chat_id=update.effective_chat.id,
text=f"Welcome, nice to meet you{os.linesep}"
f"/what would you like me to do?{os.linesep}"
)
bot = Updater(token=token, use_context=True)
bot.dispatcher.add_handler(MessageHandler(Filters.text, start_chat))
is there a filter or perhaps a handler that can alert me only in the first message in the private chat?
When a user starts a bot, /start
command will be sent to bot from user. Therefore, you have to add CommandHandler
instead of MessageHandler
.
Here's the modified code of yours:
def start_chat(update: Update, context: CallbackContext):
context.bot.send_message(
chat_id=update.effective_chat.id,
text=f"Welcome, nice to meet you{os.linesep}"
f"/what would you like me to do?{os.linesep}"
)
bot = Updater(token=token, use_context=True)
bot.dispatcher.add_handler(CommandHandler('start', start_chat))
Or you could also use MessageHandler
with Filters.regex('^/start$')
to capture the /start
command.
bot.dispatcher.add_handler(MessageHandler(Filters.regex('^/start$'), start_chat))