Telegram Bot 创建和使用教程
标签搜索
侧边栏壁纸
  • 累计撰写 22 篇文章
  • 累计收到 3 条评论

Telegram Bot 创建和使用教程

admin
2023-09-23 / 0 评论 / 67 阅读 / 正在检测是否收录...
温馨提示:
本文最后更新于2023年09月23日,已超过578天没有更新,若内容或图片失效,请留言反馈。

ae7cs-r0zxy.webp

首先你得有个telegram账号并登录客户端

  • 和官方 @BotFather 对话, 创建一个telegram bot, 在与它的对话框中输入/start /newbot 等指令, 按照提示操作, 到最后获得一个新bot, 以及一串如下格式的token:

    987654321:ABCDEF_xdfguweSAExofxT9-wdl4x

然后,用客户端给刚创建的bot发消息, 内容是什么不重要。

找到你的telegram chat_id

  • 将上面获得的token替换掉下面👇url中的TOKEN部分

    https://api.telegram.org/botTOKEN/getUpdates
    
    #替换完应该是这样的👇:
    
    https://api.telegram.org/bot987654321:ABCDEF_xdfguweSAExofxT9-wdl4x/getUpdates
  • 访问上面👆的url, 不出意外会返回如下json

    {
      "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"
                      }
                  ]
              }
          }
      ]
    }

其中的 result[0].message.chat.id 的值就是chat_id也就是 123456789

使用 bot

  • curl post 测试

    curl -s -X POST https://api.telegram.org/bot{token}/sendMessage -d chat_id={chatId} -d text="Hello World"
  • curl get 测试

    curl -s https://api.telegram.org/bot{token}/sendMessage?chat_id\={chatId}\&text\=Hello%20World

    使用实际的 tokenchat_id 替换 shell command 中的 {token}{chatId}

不出意外,你的 bot 会收到 "Hello World",恭喜,这已经成功了!

0

评论 (0)

取消