Reputation: 911
Thank you so much in advance for your help.
Based on educational website, I tried to create a simple telegram bot to post messages in Telegram group.
This is my code:
import requests
bot_id = '261600102:AAGx**************uRljA'
chat_id = '27*****'
#chat_id = "MyPythonTesting"
message = input('ENTER THE MESSAGE: ')
url = f"https://api.telegram.org/bot{bot_id}/sendmessage?chat_id={chat_id}&test={message}"
url = f"https://core.telegram.org/bot{bot_id}/sendmessage?chat_id={chat_id}&test={message}"
response = requests.get(url)
print(response)
I tried above code and getting the response as 200. My message is not getting posted in the telegram group.
I used both different urls.
For the first url,
https://api.telegram.org/bot{bot_id}/sendmessage?chat_id={chat_id}&test={message}
I get this error:
raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPSConnectionPool(host='api.telegram.org', port=443): Max retries exceeded with
url: /bot261600102:AAG********7uRljA/sendmessage?chat_id=276******&test=esae (Caused by NameResolutionError("<urllib3.connection.HTTPSConnection object at 0x000001AC0425F910>: Failed to resolve 'api.telegram.org' ([Errno 11001] getaddrinfo failed)"))
For the second url, I am getting a http 200 as response, but the message is not getting posted.
Awaiting your reply
i modified the code as below
import requests
bot_id = '261600102:AAGx**********YHCCeXV6VqD67uRljA'
chat_id = "MyPythonTesting"
message = input('ENTER THE MESSAGE: ')
url = f"https://api.telegram.org/bot{bot_id}/sendmessage?chat_id={chat_id}&text={message}"
response = requests.get(url)
print(response)
Now I am getting new Error message
C:\Users\Administrator\AppData\Local\Programs\Python\Python311\python.exe E:\pycharm\projecttelegram\MyPythonTesting_1.py
ENTER THE MESSAGE: test
Traceback (most recent call last):
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python311\Lib\site-packages\urllib3\connection.py", line 203, in _new_conn
sock = connection.create_connection(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python311\Lib\site-packages\urllib3\util\connection.py", line 60, in create_connection
for res in socket.getaddrinfo(host, port, family, socket.SOCK_STREAM):
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python311\Lib\socket.py", line 962, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
socket.gaierror: [Errno 11001] getaddrinfo failed
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python311\Lib\site-packages\urllib3\connectionpool.py", line 790, in urlopen
response = self._make_request(
^^^^^^^^^^^^^^^^^^^
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python311\Lib\site-packages\urllib3\connectionpool.py", line 491, in _make_request
raise new_e
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python311\Lib\site-packages\urllib3\connectionpool.py", line 467, in _make_request
self._validate_conn(conn)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python311\Lib\site-packages\urllib3\connectionpool.py", line 1092, in _validate_conn
conn.connect()
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python311\Lib\site-packages\urllib3\connection.py", line 611, in connect
self.sock = sock = self._new_conn()
^^^^^^^^^^^^^^^^
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python311\Lib\site-packages\urllib3\connection.py", line 210, in _new_conn
raise NameResolutionError(self.host, self, e) from e
urllib3.exceptions.NameResolutionError: <urllib3.connection.HTTPSConnection object at 0x000001EC5FA8BCD0>: Failed to resolve 'api.telegram.org' ([Errno 11001] getaddrinfo failed)
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python311\Lib\site-packages\requests\adapters.py", line 486, in send
resp = conn.urlopen(
^^^^^^^^^^^^^
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python311\Lib\site-packages\urllib3\connectionpool.py", line 844, in urlopen
retries = retries.increment(
^^^^^^^^^^^^^^^^^^
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python311\Lib\site-packages\urllib3\util\retry.py", line 515, in increment
raise MaxRetryError(_pool, url, reason) from reason # type: ignore[arg-type]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='api.telegram.org', port=443): Max retries exceeded with url: /bot261600102:AAGxPuS8CVlJB_0tZYHCCeXV6VqD67uRljA/sendmessage?chat_id=MyPythonTesting&text=test (Caused by NameResolutionError("<urllib3.connection.HTTPSConnection object at 0x000001EC5FA8BCD0>: Failed to resolve 'api.telegram.org' ([Errno 11001] getaddrinfo failed)"))
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "E:\pycharm\projecttelegram\MyPythonTesting_1.py", line 7, in <module>
response = requests.get(url)
^^^^^^^^^^^^^^^^^
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python311\Lib\site-packages\requests\api.py", line 73, in get
return request("get", url, params=params, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python311\Lib\site-packages\requests\api.py", line 59, in request
return session.request(method=method, url=url, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python311\Lib\site-packages\requests\sessions.py", line 589, in request
resp = self.send(prep, **send_kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python311\Lib\site-packages\requests\sessions.py", line 703, in send
r = adapter.send(request, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python311\Lib\site-packages\requests\adapters.py", line 519, in send
raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPSConnectionPool(host='api.telegram.org', port=443): Max retries exceeded with url: /bot261600102:AAGxPuS8CVlJB_0tZYHCCeXV6VqD67uRljA/sendmessage?chat_id=MyPythonTesting&text=test (Caused by NameResolutionError("<urllib3.connection.HTTPSConnection object at 0x000001EC5FA8BCD0>: Failed to resolve 'api.telegram.org' ([Errno 11001] getaddrinfo failed)"))
Process finished with exit code 1
Upvotes: 0
Views: 531
Reputation: 44264
First of all, don't use core.telegram.org
, if you want to use the Bot API use api.telegram.org
.
Telegram provides multiple api's, you want to use the Bot API.
Looking further to the URL you're using:
https://api.telegram.org/bot{bot_id}/sendmessage?chat_id={chat_id}&test={message}
Here you pass test
with the message
, if you look at the sendMessage
documentation, you'll see that test
is not an accepted paramater, you need to use text={message}
:
https://api.telegram.org/bot{bot_id}/sendMessage?chat_id={chat_id}&text={message}
Upvotes: 1