【PHP Telegram Bot】Day03 - 向 @BotFather 申请一只机器人吧!

讲了两天的赣话,今天终於要来实作啦!

The BotFather

https://ithelp.ithome.com.tw/upload/images/20210912/20132916IhxvW2bj9B.jpg
The Botfather 这个名字是致敬 1972 年的美国电影《教父》

它是机器人,也是机器人的管理者
几乎所有机器人的设定、建立都要透过他
用机器人设定机器人听起来就很有趣,对吧?


@BotFather 的功能

私讯 @BotFather,按「开始」後就会显示出一大堆指令,真是令人眼花撩乱
https://ithelp.ithome.com.tw/upload/images/20210912/20132916a52fuwrmmj.png
官方网页版说明:https://core.telegram.org/bots#botfather-commands

简单介绍几个比较常用的功能:

指令 说明
/newbot 建立一个新的机器人
/mybots 列出所有机器人,用按钮的方式设定机器人,而非指令(这样可以让聊天纪录变得很乾净,我比较喜欢用这个)
修改相关
/setname 修改显示名称
/setdescription 修改说明
/setabouttext 修改简介
/setuserpic 修改大头贴
/setcommands 修改指令选单
/deletebot 删除机器人
设定相关
/token 显示机器人的 token
/revoke 产生新的 token,旧的会失效
/setjoingroups 设定是否能被加入群组
/setprivacy 设定是否能读取所有聊天室讯息(机器人的讯息除外,机器人不论如何都不会看到机器人的讯息)

建立你的机器人吧

  1. 按下/newbot指令

    https://ithelp.ithome.com.tw/upload/images/20210912/2013291680v9LhTZ08.png

  2. 帮机器人取名字

    Alright, a new bot. How are we going to call it? Please choose a name for your bot.
    

    这边它会要你帮你的机器人取一个名字,这个以後能修改,所以随便取没差
    https://ithelp.ithome.com.tw/upload/images/20210912/20132916ZlBkzrKUh8.png

  3. 设定 username

    Good. Now let's choose a username for your bot. It must end in `bot`. Like this, for example: TetrisBot or tetris_bot.
    

    ⚠️ 注意:设定後就不能修改了,请谨慎思考之後设定,避免反悔时需要请使用者迁移

    需要帮你的机器人设定一个 username,必须符合以下规则:

    • 不需要打 @
    • 以 a~z 开头
    • 以 bot 结尾
    • 只能使用 a~z、0~9 和底线
    • 最小长度为 5 个字元

    显示时会区分大小写,使用时不区分大小写(例如设定成 @TetrisBot,用 @tETrISbOt 一样会指到同一个机器人)


    常见问题:

    • 不能以数字开头

      https://ithelp.ithome.com.tw/upload/images/20210912/20132916VjcnWKFrOy.png

      Sorry, this username is invalid.
      
    • 不能用 bot 以外的字结尾

      https://ithelp.ithome.com.tw/upload/images/20210912/20132916UdJ18yq0cl.png

      Sorry, the username must end in 'bot'. E.g. 'Tetris_bot' or 'Tetrisbot'
      
    • username 已经被别人先用走了

      https://ithelp.ithome.com.tw/upload/images/20210912/20132916l6hDCHkAqw.png

      Sorry, this username is already taken. Please try something different.
      

    出现 Done 表示你设定成功了
    https://ithelp.ithome.com.tw/upload/images/20210912/20132916Vs3Li26pRJ.png


使用你的机器人吧

现在你已经可以使用机器人了

按下说明里的连结(t.me/ironman2021_by_miku3920_bot 点你自己的),并且点击「开始」

然後你就会发现什麽事情都没发生,因为我们还没写机器人的程序嘛

不过就算不写程序也可以让它说话,利用昨天文章说的 HTTP GET 方法,与 sendMessage 这个 API

https://ithelp.ithome.com.tw/upload/images/20210912/20132916dOxIWMdpbw.png

https://api.telegram.org/bot{Token}/{Method}?{Parameter1}={Value1}&{Parameter2}={Value2}
  • Token1985044907:AAEdySrbzTc8tVdBjfymlfyaBUJGeu7r-v4(你的 bot token)
  • Method:sendMessage
  • Parameter1:chat_id
  • Value1:127355800(你的 uid,可以透过 @userinfobot 取得)
  • Parameter2:text
  • Value2:Hello,+World!+你好,世界!(空白要用「+」替换)

组合完会像这个样子,直接贴到浏览器的网址列就能使用罗:

https://api.telegram.org/bot1985044907:AAEdySrbzTc8tVdBjfymlfyaBUJGeu7r-v4/sendMessage?chat_id=127355800&text=Hello,+World!+你好,世界!

常见问题:

  • Token 打错

    {"ok":false,"error_code":401,"description":"Unauthorized"}
    
  • Method 打错

    {"ok":false,"error_code":404,"description":"Not Found"}
    
  • chat_id 的值不能为空

    {"ok":false,"error_code":400,"description":"Bad Request: chat_id is empty"}
    
  • chat_id 的值打错

    {"ok":false,"error_code":400,"description":"Bad Request: chat not found"}
    
  • 没私讯过机器人

    {"ok":false,"error_code":403,"description":"Forbidden: bot can't initiate conversation with a user"}
    
  • text 的值不能为空

    {"ok":false,"error_code":400,"description":"Bad Request: message text is empty"}
    

成功的话会返回机器人发送的讯息(json 格式)到网页上:

{"ok":true,"result":{"message_id":5,"from":{"id":1985044907,"is_bot":true,"first_name":"2021 iThome\u9435\u4eba\u8cfd","username":"ironman2021_by_miku3920_bot"},"chat":{"id":127355800,"first_name":"\u521d\u97f3","last_name":"\u30df\u30af","username":"miku3920","type":"private"},"date":1631440402,"text":"Hello, World! \u4f60\u597d\uff0c\u4e16\u754c\uff01"}}

https://ithelp.ithome.com.tw/upload/images/20210912/20132916VO1ZTKaOPm.png


结尾

不知不觉就写了这麽多,还有好多想讲的没讲到,今天就先讲到这里,明天继续设定机器人!


<<:  [Day12] 策略最佳化模组改造(2)

>>:  Day 12 - 阴影、透明度使用

队列

某些较耗时的工作像是寄信、发通知等,如果卡在处理请求的过程中的话就会造成使用者要多等上数秒才能收到回...

Swift纯Code之旅 Day24. 「各个TableViewHeader下的Cell显示(1)」

前言 我们已经将TableView的Header给设置完毕了,那可以看到IPhone内建的画面: 两...

Day.11 Decode String

Leetcode #394. Decode String 题目简单来说要用数字乘以[]里面的字串 e...

[Day28] AWS Cloud9

AWS Cloud9 是一种云端整合开发环境 (IDE),您只需要一个浏览器便能撰写、执行和侦错程序...

Day 25: Behavioral patterns - State

目的 如果物件内的方法,会依据物件内的状态,使用多个 if - else if - else 或 s...