Allen Hua's Another Blog
Life is going on, I do what I want.
Home
Categories
Archives
Tags
About
Home
Telegram 创建 bot 获取 token 和 chatId 以及发送消息简明教程
Telegram 创建 bot 获取 token 和 chatId 以及发送消息简明教程
取消
Telegram 创建 bot 获取 token 和 chatId 以及发送消息简明教程
由
dk11
发布于 2021-09-18
·
最后更新:2021-09-18
427
# 打开 `BotFather` url: https://t.me/BotFather 打开与它的聊天界面,不论是 Windows mac 还是 Android iOS 的 telegram 客户端。 # 创建 bot 以及获取 token 1. 输入 `/newbot` 后回车 它会回你以下内容 > Alright, a new bot. How are we going to call it? Please choose a name for your bot. 2. 这个名字是显示名称 (display name),不是唯一识别码,现在随便设置一下,之后可以通过 `/setname` 修改 比如设置成 `Zhang san's sweety bot` 3. 接着会让你设置唯一名称。字符串必须 endsWith `bot`,比如 `abc_bot` 或 `HelloWorldbot` 都是合法的。如果你设置的名字已经被占用需要重新设置。比如你设置成了 `test_bot` > Good. Now let's choose a username for your bot. It must end in `bot`. Like this, for example: TetrisBot or tetris_bot. 4. 恭喜!设置成功。会返回给你重要的 API token,务必要保存好它。另外你的 bot 的唯一 url 就已经生成: `https://t.me/test_bot` > Done! Congratulations on your new bot. You will find it at t.me/test_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: 12345678:sdfsfadsfasdfasdfasdfgdfhdfghfgh Keep your token secure and store it safely, it can be used by anyone to control your bot. > > For a description of the Bot API, see this page: https://core.telegram.org/bots/api 5. 一些其他必要的命令 - `/setdescription` 帮助你设置 bot 的描述 - `/setuserpic` 设置 bot 的头像。上传的图片 size 需要大于等于 150x150。而且上传图片需要选择压缩,不能上传文件! 此时你的 bot 很快就可以使用了。 # 获取 chatId 观察这个 url `https://api.telegram.org/bot{token}/getUpdates` 使用第二步获得的 `token` 替换上述 url 中的 `{token}` 然后得到新的 url,复制粘贴到浏览器地址栏,回车请求。不出意外你会得到如下 response ``` { "ok": true, "result": [] } ``` 很好。这时你打开你的 bot,随便和它说一句话,比如给它发一句 "Hello World",然后重新请求一遍上述的 url(替换过 token 的),不出意外你收到的 response 是这样了 ``` { "ok": true, "result": [ { "update_id": 123456, "message": { "message_id": 56, "from": { "id": 72384234234, "is_bot": false, "first_name": "sdfsdf", "last_name": "sdfsdf", "username": "sdfsdf", "language_code": "en" }, "chat": { "id": 123456789, "first_name": "sdfsdf", "last_name": "sdfsdf", "username": "sdfsdf", "type": "private" }, "date": 1212121212, "text": "/start", "entities": [ { "offset": 0, "length": 6, "type": "bot_command" } ] } }, { "update_id": 123456, "message": { "message_id": 6, "from": { "id": 23232323, "is_bot": false, "first_name": "sdfsdfsdf", "last_name": "sdfsdfsdf", "username": "sdfsdfsdf", "language_code": "en" }, "chat": { "id": 123456, "first_name": "sdfsdfsdf", "last_name": "sdfsfdsdf", "username": "sdfsdfsdf", "type": "private" }, "date": 1234567, "text": "Hello World" } } ] } ``` 如果浏览器没有安装一些 json 美化插件,会得到 json string 原文,那么复制它们到任何一个在线 json 解析格式化网站,比如 https://www.sojson.com/ 选择对应的格式化功能格式化文本。 其中的 result[0].message.chat.id 的值就是 `chatId` 也就是 `123456789` 知道了 token 和 chatId 就可以使用 bot 了。 # curl 测试 ``` curl -s -X POST https://api.telegram.org/bot{token}/sendMessage -d chat_id={chatId} -d text="Hello World" ``` 这是发送 post 请求。使用实际的 `token` 和 `chatId` 替换 shell command 中的 `{token}` 和 `{chatId}` 不出意外,你的 bot 会收到 "Hello World",恭喜,这已经成功了! # Java 测试 引入 `hutool-http`,maven based project ``` <dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-http</artifactId> <version>5.7.10</version> </dependency> ``` ``` import cn.hutool.http.HttpRequest; import java.util.HashMap; import java.util.Map; /** * @author hellodk * @date 2021-09-18 23:08 */ public class HttpPostTest { public static void main(String[] args) { String apiUrl = "https://api.telegram.org/bot%s/sendMessage"; String token = "12345:abcsdfsdf"; apiUrl = String.format(apiUrl, token); Map<String, Object> map = new HashMap<>(); String chatId = "123456"; map.put("chat_id", chatId); map.put("parse_mode", "Markdown"); map.put("disable_web_page_preview", true); map.put("text", "Hello World"); HttpRequest.post(apiUrl) .form(map) .timeout(20000) // 设置请求超时: 20s .execute() .body(); } } ``` end.
work
Telegram
Java
Linux
该博客文章由作者通过
CC BY 4.0
进行授权。
分享
最近更新
打卡南京市区人防工程纳凉点
Linux 利用 openssl 生成随机密码
收集、推荐一些白噪声网站
华强买瓜和高级特工穿山甲原版台词,要求全文背诵
OpenWrt 安装 shadow-gpasswd 从组中删除用户
热门标签
Java
笔记
随笔
日记
网络
Telegram
docker
GitHub
n1
随想
文章目录
骑行打卡江宁小川藏线之纪实|佘村之行
写了一个小项目:推送 v2ex 优惠信息节点的新帖到 telegram channel