Friday 10 February 2023

How to use Python with Telegram bot?

 step1: create a new bot with Telegram botfather

/newbot

test_bot

test_bot

(get the telegram bot token, <telegramBotToken>)


step2: check the new bot chat id, type the following url in the browser, https://api.telegram.org/bot<telegramBotToken>/getUpdates?offset=0

send a message to the new bot in telegram

refresh the browser to get the chat id

(get the telegram bot chat id, <telegramChatID>)


step3: add the bot into a new group and send a message in the group. Then, refresh step2 above

you will see the group chat id, copy and paste into your Python code

 

step4: use a Python code to verify Telegram bot

import requests

TOKEN = "<telegramBotToken>"

chat_id = "<telegramChatID>"

message = "hello from your telegram bot"

url = f"https://api.telegram.org/bot{TOKEN}/sendMessage?chat_id={chat_id}&text={message}"

print(requests.get(url).json()) # this sends the message

 


 

No comments:

Post a Comment