'telegram bot asynchronous call

I have several stages in my bot scenario:

  1. /start confirm policy agreement
  2. get_userdata for latest data sending to the server .

I implemented these two stages seperately, but struggling how to write it in one scenario calling it asynchronously my code:

get the first pending update_id, this is so we can skip over it in case we get an "Unauthorized" exception.

global UPDATE_ID
bot = telegram.Bot('........')

botel = telebot.TeleBot('.........')
try:
    UPDATE_ID = bot.get_updates()[0].update_id
except IndexError:
    UPDATE_ID = None
updater = Updater("...........")
while True:
    try:
        get_userdata(bot)
        botel.polling(none_stop=True, interval=0, timeout=0)
    except NetworkError:
        sleep(1)
    except Unauthorized:
        # The user has removed or blocked the bot.
        UPDATE_ID += 1



def get_userdata(bot: telegram.Bot) -> NoReturn:
global UPDATE_ID
for update in bot.get_updates(offset=UPDATE_ID, timeout=10):
    UPDATE_ID = update.update_id + 1
    message = {}
    if update.message.chat.username:
        message['first_name'] = update.message['chat']['first_name']
        message['last_name'] = update.message['chat']['last_name']
        message['username'] = update.message['chat']['username']
    if update.message.chat.id:
        message['chat_id'] = update.message['chat']['id']
    ...........................
    ...........................

And policy agreement button call:

def start(update: Update, context: CallbackContext) -> None:
    keyboard = [
        [
            InlineKeyboardButton("Agree with your conditions", callback_data='1')
        ]
    ]
    reply_markup = InlineKeyboardMarkup(keyboard)
    update.message.reply_text("Please press the button if you agree with conditions", reply_markup=reply_markup)


def button(update: Update, context: CallbackContext) -> None:
    query = update.callback_query
    query.answer()
    import redis
    redis = redis.Redis(host='localhost', port='6379')
    # redis.set('mykey', 'Hello from Python!')
    redis.set(str(query.message.chat.id), 'YES')
    print(str(query.message.chat.id), 'YES')
    query.edit_message_text(text=f"You agree")


def main() -> None:
"""запуск бота"""
# Create the Updater and pass it your bot's token.
updater = Updater("....................")

updater.dispatcher.add_handler(CommandHandler('start', start))
updater.dispatcher.add_handler(CallbackQueryHandler(button))


# Start the Bot
updater.start_polling()

# Run the bot until the user presses Ctrl-C or the process receives SIGINT,
# SIGTERM or SIGABRT
updater.idle()

How to combine these two scenarios in one?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source