0

so i need a specific function in python to send a message i want to my tg bot and and then be able to respond (wait for response) from tg and process that response. I know this may be a simple question i couldn't find an specific example in the documentation, given that i don't need commands or anything, just text.

I am using "telebot" but i don't mind using any other telegram bot, it's just the other apis seemed more difficult but i am good as i can get this to work.

i tried this:

def send_message_and_get_response(chat_id, message):

messages_waiting_response = {}

@bot.message_handler(commands=['start'])
def start(message):
    chat_id = message.chat.id
    print(chat_id)
    msg = bot.send_message(chat_id, "Hola, responde a este mensaje")
    messages_waiting_response[chat_id] = msg.message_id

@bot.message_handler(func=lambda message: True, content_types=['text'])
def handle_response(message):
    chat_id = message.chat.id
    if chat_id in messages_waiting_response:
        response = message.text
        original_msg_id = messages_waiting_response.pop(chat_id)   original
        bot.reply_to(message, "Has respondido: " + response)
        bot.delete_message(chat_id, original_msg_id)  
        bot.stop_polling()
        return response
bot.polling()

And bot will receive my answer but the response will never return to my main program. I need to call this function from any other function of my program, so i don't mind using asyncio either.

Any help will be appreciated!

apan524
  • 83
  • 5
germor197
  • 19
  • 1

0 Answers0