简体   繁体   中英

Starting a telegram bot without using /start command

I am building a telegram bot that shows a main menu upon starting (using /start). Now, every time I want to start the bot I have to write /start but I want to make it that if the user sent anything (without /start) the bot will start and show the main menu again. I did some research and I couldn't figure it out.

Here is my code:

from telegram.ext import CommandHandler, CallbackQueryHandler, Updater
from telegram import InlineKeyboardButton, InlineKeyboardMarkup, ReplyMarkup

API_KEY=''

def start(update, context):
  update.message.reply_text(main_menu_message(),
    reply_markup=main_menu_keyboard())

def main_menu(update,context):
  query = update.callback_query
  query.answer()
  query.edit_message_text(
    text=main_menu_message(),
    reply_markup=main_menu_keyboard())

def main_menu_keyboard():
  keyboard = [[InlineKeyboardButton('Option 1', callback_data='a1'), InlineKeyboardButton('Option 2', callback_data='a2')],
              [InlineKeyboardButton('Option 3', callback_data='a3'), InlineKeyboardButton('Option 4', callback_data='a4')]]
  return InlineKeyboardMarkup(keyboard)

def main_menu_message():
  return 'Hello, How Can I help you??'


updater = Updater(token=API_KEY, use_context=True)
updater.dispatcher.add_handler(CommandHandler('start', start))
updater.start_polling()

We can see the output of this code here

MessageHandler(Filters.all, callback) will handle any message. See here and here .


Disclaimer: I'm currently the maintainer of python-telegram-bot .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM