I am using Python for sending message to a telegram group, the URL returns 404. Below is the code:
import requests
from config import API, CHAT_ID
# telegram url
url = "https://api.telegram.org/bot{}".format(API)
print(url)
def send_mess(text):
params = {'chat_id': CHAT_ID, 'text': text}
response = requests.post(url + 'sendMessage', data=params, timeout=20)
return response
if __name__ == '__main__':
d = send_mess('Hi')
print(d)
2
Answers
it looks like the
API
or theCHAT_ID
are not well configured. Even though, I would like to suggest using thetelegram
library:pip install python-telegram-bot
There’s a simple typo in your code, lets take a look at the following lines:
This wel produce an url like;
Here, the url is missing an
/
between the token and the method, would recommend changing;url + 'sendMessage'
tourl + '/sendMessage'
to get the correct url:Then your code send the message as expected.