5

I've been trying to create a telegram bot. I coded in Python, following is what I did so far.

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

logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
                    level=logging.INFO)
logger = logging.getLogger(__name__)

@app.route("/")
def start(bot, update):
    help_text = 'text'
    keyboard = [[InlineKeyboardButton("Button_1", url=('url'))]]
    reply_markup = InlineKeyboardMarkup(keyboard)
    update.message.reply_text(help_text, reply_markup=reply_markup)


def button(bot, update):
    query = update.callback_query
    bot.edit_message_text(text="Selected option: {}".format(query.data),
                          chat_id=query.message.chat_id,
                          message_id=query.message.message_id)

def error(bot, update, error):
    logger.warning('Update "%s" caused error "%s"', update, error)


def main():
    updater = Updater(<TOKEN>)
    updater.dispatcher.add_handler(CommandHandler('start', start))
    updater.dispatcher.add_handler(CallbackQueryHandler(button))
    updater.dispatcher.add_error_handler(error)
    updater.start_polling()
    updater.idle()


if __name__ == "__main__":
    main()

I've been trying to implement user input when Button_1 is clicked i.e., on clicking Button_1 the user should receive a message with the input field (eg: please enter your favorite song). Any pointers or suggestions?

wowkin2
  • 5,895
  • 5
  • 23
  • 66
Joe
  • 291
  • 1
  • 3
  • 18

2 Answers2

0

I think you need to use ConversationHandler for that. Something like:

  • after start accept buttons
  • on Button 1 print please enter your favorite song and wait for text
  • on text print text and end conversation

See example in library you use.

wowkin2
  • 5,895
  • 5
  • 23
  • 66
0

Please take a look to the ForceReply https://core.telegram.org/bots/api#forcereply: when you send a message using sendMessage (e.g. on inline button click), you can use it to automatically open reply dialog for that message. Given this.

Alies
  • 639
  • 6
  • 11