惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

WordPress大学
WordPress大学
Cyberwarzone
Cyberwarzone
The GitHub Blog
The GitHub Blog
云风的 BLOG
云风的 BLOG
P
Proofpoint News Feed
小众软件
小众软件
Recent Announcements
Recent Announcements
博客园 - 三生石上(FineUI控件)
Security Archives - TechRepublic
Security Archives - TechRepublic
W
WeLiveSecurity
Cloudbric
Cloudbric
博客园 - 司徒正美
美团技术团队
N
News and Events Feed by Topic
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
PCI Perspectives
PCI Perspectives
宝玉的分享
宝玉的分享
H
Help Net Security
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Google DeepMind News
Google DeepMind News
Help Net Security
Help Net Security
Last Week in AI
Last Week in AI
S
Schneier on Security
N
News | PayPal Newsroom
B
Blog RSS Feed
L
LINUX DO - 最新话题
T
Troy Hunt's Blog
S
Secure Thoughts
雷峰网
雷峰网
aimingoo的专栏
aimingoo的专栏
L
Lohrmann on Cybersecurity
G
Google Developers Blog
Microsoft Azure Blog
Microsoft Azure Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
T
Tenable Blog
S
Securelist
L
LangChain Blog
Recent Commits to openclaw:main
Recent Commits to openclaw:main
I
InfoQ
H
Heimdal Security Blog
Cisco Talos Blog
Cisco Talos Blog
F
Full Disclosure
Y
Y Combinator Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
K
Kaspersky official blog
T
Tailwind CSS Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
阮一峰的网络日志
阮一峰的网络日志
C
Cisco Blogs

Allen Hua 的网络博客

由于 Linux 桌面没有一个好用的天气程序就写了一个跨平台的CheckitoutWeather 给 caesium-image-compressor 图片压缩程序构建了 Linux AppImage v2.8.5 最新版 debian13(debian trixie)安装了nvidia闭源驱动后从x11切换到wayland的方法 山间摩旅追风,偶遇一场绚烂晚霞 最近使用debian系统的一些心得 机械革命无界14Pro笔记本debian forky成功驱动内置扬声器和麦克风 记录 typecho 1.2.0 升级到 1.3.0 过程 开发了一款openwrt插件:文本剪贴板 解决机械革命笔记本内屏高刷240Hz闪屏问题 使用 Java 写了一个局域网端口扫描器 - Allen Hua 的网络博客 openwrt使用外置根extroot机制扩展根分区大小 - Allen Hua 的网络博客 将机场ss节点批量转换成ss字符串链接批量添加到passwall - Allen Hua 的网络博客 给机械革命钛钽plus换屏:NY2换成NZ2 - Allen Hua 的网络博客 记录一次pve宿主机和上面的debian虚机无故down机事件 - Allen Hua 的网络博客 给图床部署cdn腾讯云的edgeone并排查Cache-Control max-age 3600的问题 - Allen Hua 的网络博客 从高山草甸到徽派古村:武功山反穿与皖浙赣自驾行记 - Allen Hua 的网络博客 增程器就是充电宝?别被忽悠了 - Allen Hua 的网络博客 浦口龙虎巷扫街,记录人间真实 博客图片压缩方案更新|AVIF|WebP|MozJPEG|标准JPEG Windows 电脑使用 Obs Studio 录制各个网站视频/桌面画面教程 为typecho博客添加latex支持 新能源汽车之纯电车使用交流慢充和直流快充的充电损耗对比 2025年3月更新全国5A景区名录 2025最新查看小米/红米手机电池健康度和循环次数方法 完美解决 seafile FILE_SERVER_ROOT 配置导致的内网外网不能同时访问和上传下载的问题 纯css实现typecho博客文章文字spoiler剧透效果 我对Typecho Facile主题的一些修改,图片懒加载优化,样式定制 2024年8月我的宜昌 - 重庆 - 川西小环线自驾旅行分享 2022年打卡南京市区人防工程纳凉点 2024年带着A7C2+腾龙28-200 再次来到红山森林动物园 入手a7c2第一个变焦镜头腾龙28-200体验分享 使用 exiftool 方便快捷写入图片的 exif metadata 使用JavaScript抓取易车销量排行榜数据到一个页面上展示 使用JavaScript脚本快速获取B站观看历史记录页面的视频标题和URL 入门三脚架推荐|云腾VCT-668配合索尼A7C2+FE 35mm F1.8 第一次夜景出片 Windows 11 Docker Desktop 更换默认C盘存储路径,释放C盘空间 雨游钟山——雨天带着索尼A7C2去钟山拍照 带上A7C2第一次扫街,分享今天拍的一些片子 再次为尼,买了人生第二台相机——索尼A7C2和FE 35mm F1.8 定焦镜头 更新 link-cleaner v1.1.0 适配linux桌面环境 使用自定义JavaScript脚本功能禁止memos最新版默认打开的链接预览功能 debian 11升级 docker ce 版本和 docker-compose 版本 机械革命无界14 Pro 安装 debian testing/trixie/sid 驱动扬声器,外部耳机,外接显示器过程分享 不借助 mirror 仓库,为docker engine daemon添加 proxy 访问 docker hub,解决国内公开的docker mirror 仓库不再可用了的问题 徒步江苏镇江句容九连尖 macOS 安装 proxychains-ng 并借助局域网 socks5 代理使用 Google Earth Pro 修复一块 18 年 12 月买的二手 4TB 机械硬盘 iCloud 在 Windows 下很流氓,用 taskkill 干掉它 2023 年 12 月 Spotify 彻底被墙,我在比亚迪车机上安装 surfboard 挂代理听歌 利用 proxychains 让 Windows10/11 上的 Google Earth Pro 通过代理正常访问 湖州安吉天荒坪盘山公路和杭州临安浙西天路之旅 Redmi K70 首发开箱和拍照体验 Review Android 13+ 通过 Shizuku + Language Selector 给特定应用设置独立语言 我的 2023 Spotify Wrapped 听歌总结 压缩剪贴板内图片并立即写入剪贴板,减轻服务器存储压力 【Allen Hua 翻唱】山阴路的夏天和一个人的北京 初涉录音设备,记录我购买声卡和麦克风的曲折经历以及第一首翻唱陶喆——流沙 soul power 版本(low quality) 特种兵旅行之马鞍山一日游 给我的 .com 域名续期以及转移域名(GoDaddy 到 Cloudflare) 看《下辈子我再好好过》第三季 使用油猴脚本干掉B站web首页的浏览器插件警告信息 闯进我平静生活的三只猫 在远离中短视频几天之后 [可能是]最好的压缩图片程序,使用 squoosh-cli 批量压缩图片 再次出发,自驾徽州天路和黄山之行 nginx设置referer配置个人图床的防盗链功能 微博注销记 第一次4+2旅行,骑行皖南川藏线 让 rime 输入法用上颜文字 使用 nginx 获取自己的公网 IP 地址 2023 年 618 买了一台机械革命笔记本,但很不愉快 聊聊我晚睡的习惯和早点洗澡悟出来的原因 配置 ShadowsocksR Plus+ 的服务端,在外访问家中服务 部署一个 memos 作为个人微博站点和备忘录 我的听歌软件 Spotify 和网易云的故事 | 导出网易云的歌单再导入到 Spotify 我问 ChatGPT 为何印度人痔疮发病率低 使用 immich 来管理家庭照片 | 升级 immich 1.52.0 添加 typesense 容器 在 OpenWrt 上搭建 nfs server 利用 cron 和 curl 自动模拟登录馒头,防止账号被 ban 更新 Mattermost 服务端版本,由 7.4.0 更新到 7.8.0 获取 Seafile 分享的文件直链 你是科技产品偏执狂吗?一张不同种类互联网用户日常使用产品的分级图片 记录我的 V2EX 账号被封 不通过插件给 Typecho 博客添加图片灯箱功能(fancybox js 实现) 为 Typecho 博客添加 favicon.ico(网页标签小图标) OpenWrt 修改登录页面默认展示的 root 用户名信息,保护隐私 米家智能插座 3 上手初体验 互联网上为了保护隐私我们能做些什么?分享我的想法和我写的 link-cleaner tplink 10280, wan 无法访问 lan 下的 dhcp 主机,换上 padavan 解决两台路由器 lan-wan 级联,两台路由器下面的电脑互访 机械革命自带键盘总是自动乱按,拆机拔掉排线,完美解决烦恼 做了一个 squoosh docker 镜像,写博客贴图直接在本地压缩文件,安全且高效 nginx 常规端口和非常规端口的 http redirect to https 记一次 OpenWrt luci 显示正在“收集数据”,使用公网 IP nginx 反向代理遇到的 Uncaught TypeError: Cannot read properties of null (reading 'wan') 问题 OpenWrt 安装并配置腾讯云动态 dns dnspod tencent ddns 浅谈文件传输/共享协议|内网共享文件协议选择|自建公网文件管理同步和共享方案推荐 解决 webdav 连接 seafile 无法重命名文件等操作造成的 SeafDAV: MOVE returns 502 Bad Gateway 错误 Linux 删除当前目录下的隐藏文件 OpenWrt 安装 4.4.2 版本的 qbittorrent-nox Docker 构建多架构镜像实战 构建 amd64 和 arm64 架构的兰空图床镜像 构建 arm64 架构和 amd64 架构的兰空图床 docker 镜像
Telegram 创建 bot 获取 token 和 chatId 以及发送消息简明教程
Allen Hua · 2021-09-18 · via Allen Hua 的网络博客
阅读量:46857

warning: 这篇文章距离上次修改已过1711天,其中的内容可能已经有所变动。

打开 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_botHelloWorldbot 都是合法的。如果你设置的名字已经被占用需要重新设置。比如你设置成了 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 请求。使用实际的 tokenchatId 替换 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.