python如何快速创建一个telegram 机器人bot
1.打开telegram,搜索关注BotFather
2.
1️⃣/start
2️⃣/newbot
3️⃣命名bot的名字
4️⃣命名自己的名字
5️⃣Use this token to access the HTTP API
6️⃣https://api.telegram.org/bot(token)/getMe →获得bot信息
7️⃣把上面的getme改成getupdates 获得与机器人的聊天记录和自己的ID
8️⃣https://api.telegram.org/bot(token)/sendMessage?chat_id=(自己的ID)&text=消息
3.用python让机器人推送(给自己发消息)
code:【记得改token和chat_id】
import requests
def telegram(self, msg):
print(msg)
config_dict = {"telegram": {
"token": "", "chat_id": ""}}
try:
url = "https://api.telegram.org/bot%s/sendMessage" % (
config_dict["telegram"]["token"])
param = {"chat_id": config_dict["telegram"]["chat_id"], "text": msg, }
result = requests.post(url, param, timeout=2)
print(msg)
except Exception as e:
print('send_telegram_msg get exception:', e)