3

I have a piece of code to test that my implementation of the telegram bot is working in python. It works completely fine on my Windows 11 laptop, but when I run it on a Windows 2019 server, I get the following output:

c:\Python\Scripts\telegram_test.py:11: RuntimeWarning: coroutine 'Bot.send_message' was never awaited bot.sendMessage(chat_id=chat_id, text=msg) RuntimeWarning: Enable tracemalloc to get the object allocation traceback Message Sent!

Both installs are using python 3.9.0 and I have confirmed they are both using telegram 0.0.1, so the error is a bit confusing. I also don't use async in the code as you can see here:

import telegram

my_token = 'blahblahblah'

def send(msg, chat_id, token=my_token):
    """
    Send a message to a telegram user or group specified on chatId
    chat_id must be a number!
    """
    bot = telegram.Bot(token=token)
    bot.sendMessage(chat_id=chat_id, text=msg)
    print('Message Sent!')


MessageString = 'Testing from virtual server'
print(MessageString)
send(MessageString, '-blahblah', my_token ) 

There is really nothing to the code at all and it 100% works every time from my laptop, so I have no idea what the difference is. Any thoughts?

Andy B
  • 127
  • 8

1 Answers1

6

Okay, I have it working thanks to this post:

Telethon leads to `RuntimeWarning: coroutine 'MessageMethods.send_message' was never awaited`

I don't know why it was not working previously, but after amending the code as follows, it works:

import telegram
import asyncio

my_token = 'blahblahblah'
my_chat_id = -123456789

async def send(msg, chat_id, token=my_token):
    """
    Send a message "msg" to a telegram user or group specified by "chat_id"
    msg         [str]: Text of the message to be sent. Max 4096 characters after entities parsing.
    chat_id [int/str]: Unique identifier for the target chat or username of the target channel (in the format @channelusername)
    token       [str]: Bot's unique authentication token.
    """
    bot = telegram.Bot(token=token)
    await bot.sendMessage(chat_id=chat_id, text=msg)
    print('Message Sent!')


MessageString = 'Testing from virtual server'
print(MessageString)
asyncio.run(send(msg=MessageString, chat_id=my_chat_id, token=my_token))
Suuuehgi
  • 4,547
  • 3
  • 27
  • 32
Andy B
  • 127
  • 8
  • Thanks saved me a lot of time. Just a small observation, why don't you edit your my_token variable to be a numeric string? Would be closer to what's needed, and why don't you use that same global variable in the last line asyncio.run(send command? – Robert Alexander Jun 16 '23 at 14:55
  • 2
    @RobertAlexander **1:** `token` is [expected to be a string](https://docs.python-telegram-bot.org/en/stable/telegram.bot.html#telegram.Bot), while `chat_id` can be [either an integer or a string](https://docs.python-telegram-bot.org/en/stable/telegram.bot.html#telegram.Bot.send_message). **2:** The first string, `'-blahblahblah'`, is the (invalid) `chat_id`. `chat_id` and `my_token` just happen to coincide in this example. I will propose a fix for this in the answer above. – Suuuehgi Jul 01 '23 at 09:17