skip to Main Content

Using python-telegram-bot, I recently installed the –pre version which broke this code.
It seems now it wants everything to be async. In this example, I use code that has nothing to do with telegram bot to decide what will be the value of mymsg, so I don’t need/want to havea commandHandler, I just want to be able to send a message in a Telegram channel whenever I decide so in my code

async def teleg_mail(msg):
    bot = telegram.Bot('TOKEN')
    keyboard = [[InlineKeyboardButton("Publish", callback_data='1///'+msg)]]
    reply_markup = InlineKeyboardMarkup(keyboard)
    await bot.send_message('CHANNEL',msg, reply_markup=reply_markup)

def main() -> None:
    #...
    mymsg="code above will decide what's in this string"
    teleg_mail(mymsg)

if __name__ == "__main__":
    main()

I used async and await because I was getting error, but even when using it, now I get

 RuntimeWarning: coroutine 'teleg_mail' was never awaited

And I cannot use await on teleg_mail() because it’s not in an async function…
How can I solve this?

2

Answers


  1. import asyncio
    
    async def main() -> None:
        #...
        mymsg="code above will decide what's in this string"
        await teleg_mail(mymsg)
    
    
    if __name__ == "__main__":
        loop = asyncio.get_event_loop()
        loop.run_until_complete(main())
    
    Login or Signup to reply.
  2. For switching from v13.x to v20.x, I highly recommend reading the v20.0a0 release notes as well as the transition guide. Please also note that the recommended way to use the bot methods without handlers is descriped here.


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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search