现在已经申请了一个Bot
,申请方法很简答, 在电报中 搜索 BotFather, 按照提示,直接申请即可。
最后得到一个token
。
Use this token to access the HTTP API:
714712916:AAEFMPiGY-Vi8swNb8dQCkZJioR0Y9odFcI
准备工作
需要一个国外的vps,安装了web服务器并且有证书。
这个好办。 3.5美元的一个月的vultr并不是很难接受,可使用我的邀请链接https://www.vultr.com/?ref=7613464,注册都会送25美元,够用几个月了.免费证书 let’s encrypt 了解下。
配置好 nginx + let’s encrypt
server {
listen 80;
server_name botapi.safecode.cc;
rewrite ^(.*)$ https://$host$1 permanent;
}
server {
listen 443 ssl;
ssl on;
#Nginx 所需要 ssl_certificate 文件
ssl_certificate /etc/letsencrypt/live/botapi.safecode.cc/fullchain.pem;
#安全证书 KEY 文件;
ssl_certificate_key /etc/letsencrypt/live/botapi.safecode.cc/privkey.pem;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
ssl_ciphers AES256+EECDH:AES256+EDH:!aNULL;
server_name botapi.safecode.cc;
index index.html index.htm index.php default.html default.htm default.php;
root /usr/share/nginx/botapi;
location ~* \.php$ {
fastcgi_pass unix:/run/php-fpm/fpm-www.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
注册 webhook
让电报知道我们使用那个地址来接受消息
https://api.telegram.org/bot714712916:AAEFMPiGY-Vi8swNb8dQCkZJioR0Y9odFcI/setWebhook?url=https://botapi.safecode.cc/index.php
https://api.telegram.org/bot{TOKEN}/setWebhook?url={接口地址}
返回结果
{"ok":true,"result":true,"description":"Webhook was set"}
访问下面链接
https://api.telegram.org/bot714712916:AAEFMPiGY-Vi8swNb8dQCkZJioR0Y9odFcI/getWebhookInfo
https://api.telegram.org/bot{TOKEN}/getWebhookInfo
返回
{"ok":true,"result":{"url":"https://botapi.safecode.cc/index.php","has_custom_certificate":false,"pending_update_count":0,"last_error_date":1543936592,"last_error_message":"SSL error {error:1416F086:SSL routines:tls_process_server_certificate:certificate verify failed}","max_connections":40}}
删除Webhook
deleteWebhook
使用该接口移除已经集成的webhook
,该请求没有参数
Use this method to remove webhook integration if you decide to switch back to getUpdates. Returns True on success. Requires no parameters.
https://api.telegram.org/bot714712916:AAEFMPiGY-Vi8swNb8dQCkZJioR0Y9odFcI/deleteWebhook
https://api.telegram.org/bot{TOKEN}/deleteWebhook
接口体验
简单说, 机器人就是通过接口获取消息,进行处理,或者推送消息到电报, 所有操作都是基于接口的。
通过借口的学习,应该就能掌握电报机器人的开发。
- 获取机器人收到的数据
# 先将index.php内容修改为如下:
<?php
file_put_contents('./log.txt', file_get_contents('php://input'), FILE_APPEND);
然后给bot
发送消息,再执行命令
[root@safecode botapi]# cat log.txt
{"update_id":817792877,"message":{"message_id":38,"from":{"id":669057135,"is_bot":false,"first_name":"ker","last_name":"win","username":"webinf","language_code"
:"zh-hans"},"chat":{"id":-303225768,"title":"\u5929\u5929\u6f02\u7ea2\uff5e","type":"group","all_members_are_administrators":true},"date":1544090470,"text":"hello test"}}
[root@safecode botapi]#
第一个机器人
收到消息后,回复名字
#修改 index.php代码
<?php
define ('url',"https://api.telegram.org/bot714712916:AAEFMPiGY-Vi8swNb8dQCkZJioR0Y9odFcI/");
$update = json_decode(file_get_contents('php://input') ,true);
$chat_id = $update['message']['chat']['id'];
$name = $update['message']['from']['first_name'];
$message = 'Hi '.$name;
file_get_contents(url."sendmessage?text=".$message."&chat_id=".$chat_id);
很傻,但是已经可以自动回复了。一个最简答的bot
只需要6行代码