为了解决Telegram中文搜索相关问题而写的机器人,可以称之为复读机,后面可能添加更多功能。
写了一篇博客文章记录了一下过程,供参考:https://taosky.github.io/post/telegram-robot/
- 消息记录、搜索、复读定位(主要功能,解决中文搜索的问题)。
- 定时任务(有能力可根据需要修改,目前是报时,删除文件可取消)。
- 具有定时撤回、排除ID等额外配置。
@your_bot {keyword} {page}
: 用于搜索,@
无参数为显示历史消息,此时翻页用* {page}
,无页码默认第一页,pagesize
可自行设置。/chatid
: 获取当前聊天的ID,即Group ID或User ID,此功能可在多个聊天中独立使用。/help
: 获取使用帮助。
- VPS/其他主机
- Python环境
- Web Server(可选,WebHook模式需要)
可以直接Docker部署:https://hub.docker.com/r/taoskycn/telegram-search-bot
- 虚拟环境(可选)
- 安装依赖:
pip install -r requirements.txt
。 - 修改
config.py
。 - 轮询模式直接
python robot.py
启动即可;如果需要WebHook模式,根据情况修改robot.py
最后几行,并设置好Nginx
等Web Server,比较麻烦请自行了解。
- 与@botfather对话按照步骤创建Bot,记录
token
备用。 - 设置Inline Mode: 选择你的Bot进入Bot Settings,Inline Mode开启,Edit inline placeholder,发送
{keywords} {page}
。 - 关闭Privacy mode,选择你的Bot进入Bot Settings,Group Privacy - Turn off。
- 按照喜好设置其他选项,将Bot添加到Group。
- 修改
config.py
中TOKEN
运行代码,在Group中使用/chatid
获得Group ID
,修改config.py
中的GROUP_ID
,重新运行代码即可正常使用。
- Inline Mode具有缓存效果,故连续重复搜索可能不会加载新的消息。
- Inline Mode placeholder更新需要重启客户端。
user_handlers
中的模块用于接受群组消息实现一些功能,只需要实现回调函数并添加handler
即可添加一个模块(默认的text
已被占用,如需要修改msg_store.py
)。
user_jobs
中的模块用于实现定时任务(间隔运行,定时需手动判断),同上,添加job_info
即可。
- 支持多关键词搜索
- 修复inline mode部分关键词结果不显示问题(特定字符导致的解析错误)
- 添加代理选项(酸酸乳的socks5貌似不行,http可用)
- 修复重复报时。
- 完善README。
- 修复搜索的页码问题。
- 重写了大量代码,更换MYSQL数据库为SQLITE,使用ORM,简化后续的开发及方便用户配置。
- 增加排除ID的配置。
- 增加图片、视频、语音、音频的复读。
- 增加群员获取数据库的命令。
- 存储信息过程中过滤机器人的信息。
- Bot的用户名无需手动设置。
- 修复管理员权限模式下的无权限不能复读的问题。