一、加入 BotFather 机器人
点击网址https://t.me/BotFather ,打开与它的聊天界面。
二、创建 bot 并获取 token
2.1 创建机器人
输入 /newbot 回车
显示:Alright, a new bot. How are we going to call it? Please choose a name for your bot.
2.2 输入机器人的名称
比如输入 winamp bot 回车
显示:Good. Now let’s choose a username for your bot. It must end in bot. Like this, for example: TetrisBot or tetris_bot.
2.3 输入唯一的机器人用户名
格式为winamp_bot 或 winampbot 必须以bot结尾。
失败后显示:Sorry, xxxxxxxxxx
成功后显示:
Done! Congratulations on your new bot. You will find it at t.me/winamp_bot. You can now add a description, about section and profile picture for your bot, see /help for a list of commands. By the way, when you’ve finished creating your cool bot, ping our Bot Support if you want a better username for it. Just make sure the bot is fully operational before you do this.
Use this token to access the HTTP API:
5538174337:AAHCLfyReMOfeLnp_znYWsICjTd1e3ApRGg
Keep your token secure and store it safely, it can be used by anyone to control your bot.
2.4 提取token
2.3中HTTP API 下面一行就是需要的token。
三、获取chat_id
3.1先测试一下
浏览器中输入:https://api.telegram.org/bot{token}/getUpdates 回车
其中:{token}为2.4中获取的token,包括大括号。
显示:{
“ok”: true,
“result”: []
}
如果显示error,说明有错误。
3.2 获取chat_id
3.2.1 在你生成的机器人中(本例为winamp bot的机器人)随意输入一个词语,比如“Hello World”。
3.2.2 重新在浏览器中输入https://api.telegram.org/bot{token}/getUpdates
其中:{token}为2.4中获取的token,包括大括号。
3.2.3 在显示的ok页中找到”chat”: {“id”: 123456789,”first_name”…….其中id后的数字即为需要的chat_id。
3.3 curl 测试一下获取到的taken和chat_id
在vps中输入命令
curl -s -X POST https://api.telegram.org/bot{token}/sendMessage -d chat_id={chatId} -d text="Hello World"
其中:{token}、{chatId}分别为获取的token和chatid,包括大括号。
3.4 成功与否
Telegrame客户端中的winamp bot收到”Hello World”,就成功了!