Telegram Bot

创建 Bot

使用 @BotFather 创建一个新的 Bot,按照提示操作,获取 Bot 的 API Token 并妥善保存。

获取 Chat ID

  1. 进入 Bot 聊天界面,发送一条消息,例如 “Hello”。

  2. 访问 Telegram API 获取更新: https://api.telegram.org/bot<Bot-Token>/getUpdates

  3. 返回结果中的 chat.id 即为 Chat ID。

    JSON
    {
      "ok": true,
      "result": [
        {
          "update_id": 123456789,
          "message": {
            "message_id": 1,
            "from": {
              "id": 123456789,
              "is_bot": false,
              "first_name": "EwigLicht",
              "username": "EwigLicht",
              "language_code": "zh-hans"
            },
            "chat": {
              "id": 123456789,
              "first_name": "EwigLicht",
              "username": "EwigLicht",
              "type": "private"
            },
            "date": 123456789,
            "text": "Hello"
          }
        }
      ]
    }
    点击展开查看更多

发送消息

appleboy/telegram-action

YAML
    steps:
      - name: Notify
        if: ${{ always() && env.TELEGRAM_BOT_TOKEN && env.TELEGRAM_CHAT_ID }}
        uses: appleboy/telegram-action@master
        with:
          to: ${{ secrets.TELEGRAM_CHAT_ID }}
          token: ${{ secrets.TELEGRAM_BOT_TOKEN }}
          format: markdown
          message: |
            *标题*

            ${{ steps.checkin.outputs.result  }}

            [任务详情](https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }})
点击展开查看更多

curl

BASH
curl -sS -X POST "https://api.telegram.org/bot<Bot-Token>/sendMessage" \
     -H "Content-Type: application/json" \
     -d '{
           "chat_id": "<Chat-ID>",
           "text": "Hello, World!"
         }'
点击展开查看更多

版权声明

作者: EwigLicht

链接: https://ewigl.github.io/posts/telegram-bot/

许可证: CC BY-NC-SA 4.0

本作品采用 CC BY-NC-SA 4.0 国际许可协议进行许可。请注明来源,非商业性使用,并保持相同的许可协议。 This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. Please attribute the source, use non-commercially, and maintain the same license.

开始搜索

输入关键词搜索文章内容

↑↓
ESC
⌘K 快捷键