python tg机器人

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])
posted @   是谁走漏了消息  阅读(545)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示