1

I'm trying to build a python script utilising a Telegram bot to enable the following process:

  1. Get the details of a potential bet on a sport event from a database
  2. Notify me of the bet details via a Telegram message
  3. Place a corresponding bet through a bookmaker's website (manual step)
  4. Input the details of the bet I place into the bot
  5. Extract the bet details from the bot and store them in a database

Below is the main function that will use the bot to enable the process above:

def process_bets():
    updater = create_updater()
    while True:
        # There will be code here to query a database for a viable bet
        # I've created some dummy variables to simulate a successfully returned query
        dummy_bet_id = 123
        dummy_bookie_name = "Pinnacle"
        dummy_target_odds = 1.18
        dummy_target_stake = 50
        msg = (
            "You have a new bet:\n"
            f"bet_id: {dummy_bet_id}\n"
            f"bookie_name: {dummy_bookie_name}\n"
            f"target_odds: {dummy_target_odds}\n"
            f"target_stake: {dummy_target_stake}\n"
        )
        updater.bot.send_message(chat_id=<MY_USER_ID>, text=msg)
        while True:
            # a. loop checking that the user has entered *complete* bet details into bot - assume 3 variables: 'bookmaker', 'odds' and 'stake'
            # b. extract bet details from bot into a variable, say a dictionary
            # c. store bet details in database
            # d. wipe the bet details inside the bot ready for the next bet
            # e. break out of this inner loop
        time.sleep(5)

To save space in this post I've not included all the details of the updater I've created but it follows pretty much the same format as this example on GitHub.

I need help on points a and b above as I can't work out how to extract the bet details from the bot as a variable that I can further process. All the posts I've seen on how to deal with user input provide solutions that sit inside the bot's message handling functions. However, bets can only be processed consecutively so I need to store the details of any bet I place before querying the database again for another bet.

Thanks in advance!

Oh and bonus points if you're able to give me a hint on how to do d or if it is even necessary!

Jossy
  • 589
  • 2
  • 12
  • 36
  • since you're apparently using `python-telegram-bot` and already linked to the PTB examples, I'll strongly suggest to read [this tutorial](https://github.com/python-telegram-bot/python-telegram-bot/wiki/Extensions-–-Your-first-Bot). I'm suggesting this because you build an `updater` don't use any of its functionality. – CallMeStag Mar 01 '22 at 07:20
  • Hi. There's a large amount of functionality in the updater as there is in the example I linked to - I make use of multiple `MessageHandler` classes inside of a `ConversationHandler`. As per my post I didn't include it because there's so much of it. My issue is that I don't think I can locate any of the database interaction code inside of the bot/updater because the whole betting process needs to be sequential. Are you thinking there is a way of managing this sequentially by placing the database interaction code inside the bot? – Jossy Mar 01 '22 at 17:39
  • The only way I could figure out how to include everything inside the bot would be to include all the bet generation code too - this would need to run in a sequential loop. I looked at repeating job queues but these are for fixed intervals and the time it takes for me to place a bet can vary. I was concerned things would get confused if I was still in the process of placing a bet when the next job kicked off – Jossy Mar 01 '22 at 17:49

0 Answers0