iT邦幫忙

2021 iThome 鐵人賽

DAY 3
0
Software Development

用 PHP 打造專屬於自己的 Telegram 聊天機器人吧!系列 第 3

【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


結尾

不知不覺就寫了這麼多,還有好多想講的沒講到,今天就先講到這裡,明天繼續設定機器人!


上一篇
【PHP Telegram Bot】Day02 - Telegram 聊天機器人原理
下一篇
【PHP Telegram Bot】Day04 - Telegram 機器人的設定
系列文
用 PHP 打造專屬於自己的 Telegram 聊天機器人吧!30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 則留言

1
翗祤
iT邦新手 5 級 ‧ 2021-09-27 15:01:03

感謝教學!成功用bot發出第一則訊息好有成就感XD

我要留言

立即登入留言