tg机器人
tg机器人官方api文档
https://core.telegram.org/bots/api#getfile
实例代码
import requests, json, time, os
class my_reboot():
def __init__(self):
self.token = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
self.base_file = os.path.split(os.path.realpath(__file__))[0]
self.file_path = os.path.join(self.base_file, "file")
if not os.path.exists(self.file_path):
os.mkdir(self.file_path)
def my_getFile(self, file_id): # 获取群组里的文件
url = "https://api.telegram.org/bot{}/getFile?file_id={}".format(self.token, file_id)
ret = requests.get(url=url)
my_dict = json.loads(ret.content.decode())
file_path = my_dict["result"]["file_path"]
file_path_url = "https://api.telegram.org/file/bot{}/{}".format(self.token, file_path)
# print(file_path_url)
os.system("wget {}".format(file_path_url))
def my_getUpdates(self): # 获取群组里的消息
url = "https://api.telegram.org/bot{}/getUpdates".format(self.token)
ret = requests.get(url=url)
my_dict = json.loads(ret.content.decode())
if len(my_dict["result"])>0:
# 处理时间戳,以消息时间为保存名称
data_time = my_dict["result"][0]["message"]["date"]
timeArray = time.localtime(data_time)
otherStyleTime = time.strftime("%Y-%m-%d_%H-%M-%S", timeArray)
# 展开数据,方便查看
data = json.dumps(my_dict, sort_keys=True, indent=4, ensure_ascii=False)
if not os.path.exists(os.path.join(self.file_path, otherStyleTime+".json")):
with open(os.path.join(self.file_path, otherStyleTime+".json"), "w", encoding="utf-8") as f:
f.write(data)
# a = my_reboot()
# a.my_getUpdates()
# a.my_getFile(file_id="AgACAgUAAxUAAV7h6CTeFoOOdoMnL7T4biAo8fBzAAKppzEbwQABixn0BaBRevC0bC8WzDIABAEAAwIAA2EAA2GtAAIaBA")
print(os.path.split(os.path.realpath(__file__))[0])