Search code examples
pythonraspberry-pitelegram

Unable to get the same output from a python telegram bot between 2 linux computers


I have a python telegram bot running on my laptop. I am trying to run the same script on my Raspberry Pi4 but i am getting an warning message when I try to run it which isnt happening on the laptop. I am using python 3.9 on both systems and I have installed the same packages.

On the laptop this works fine, however when I ssh to the Pi to run it I get the warning below, when running direct from command line and through vscode ssh.

This is the warning message 'RuntimeWarning: coroutine 'Bot.send_message' was never awaited bot.send_message(chat_id=chat_id, text=f'{message}') RuntimeWarning: Enable tracemalloc to get the object allocation traceback'

Any help will be greatly appreciated, thank you.

#  Telegram bot practice
from bs4 import BeautifulSoup as bs
from urllib import request
import telegram
import json


chat_id = '********'
token = '***********************************'
bot = telegram.Bot(token=token)


def send_message(message):
    bot.send_message(chat_id=chat_id, text=f'{message}')

send_message('hey there')

Solution

  • I now have it working, I imported asyncio and used asyncio.run for the sending of the message.

    I still have no idea why i had this issue on one system and not the oter buy either way it is now working.

        from bs4 import BeautifulSoup as bs
    from urllib import request
    import telegram
    import json
    import asyncio
    
    chat_id = '********'
    token = '***********************************'
    bot = telegram.Bot(token=token)
    
    
    async def send_message(message):
          bot.send_message(chat_id=chat_id, text=f'{message}')
    
    asyncio.run(send_message('hey there'))