利用telegram bot定时推送消息
有时候手机不在身边或者在开会什么的,不是很紧急的事情,闹钟就很尴尬,于是有了创建bot定时推送消息达到提醒的想法
创建bot
点击:https://telegram.me/botfather
或者tg搜索botfather
依次输入:
/start
/newbot
按步骤设置bot名称等
最终获得API,输出如下:
Use this token to access the HTTP API:
8888888888:AAHAUPoCt9IjH0F237911iotchCpGqZtLUU //这个就是API
Keep your token secure and store it safely, it can be used by anyone to control your bot.
同时开启群组权限
/setprivacy
选择刚刚创建的bot
改为:Disable
创建群组
1.新建一个群组
2.搜索你的机器人
3.将机器人拉进群组
4.获取群组ID,浏览器访问
https://api.telegram.org/bot8888888888:AAHAUPoCt9IjH0F237911iotchCpGqZtLUU/getUpdates
找到这条
"chat":{"id":-666666666,"title":"groupname","type":"group","all_members_are_administrators":true}
-666666666
即为群组ID
测试消息
curl:
curl -k --data chat_id="-666666666" --data "text=测试" "https://api.telegram.org/bot8888888888:AAHAUPoCt9IjH0F237911iotchCpGqZtLUU/sendMessage"
浏览器:
https://api.telegram.org/bot8888888888:AAHAUPoCt9IjH0F237911iotchCpGqZtLUU/sendMessage?chat_id=-666666666&text=测试
群组就能收到bot消息了
定时推送
我用了个最简单的方式,指定时间执行curlcrontab -e
:
01 0 * * * curl -k --data chat_id="-666666666" --data "text=测试" "https://api.telegram.org/bot8888888888:AAHAUPoCt9IjH0F237911iotchCpGqZtLUU/sendMessage"