iT邦幫忙

2021 iThome 鐵人賽

DAY 24
0
Software Development

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

【PHP Telegram Bot】Day24 - InlineKeyboardMarkup、Deep Link:來玩玩訊息下方的按鈕吧

https://ithelp.ithome.com.tw/upload/images/20211003/20132916dRoDxjdpup.png

InlineKeyboardMarkup

https://core.telegram.org/bots/api#inlinekeyboardmarkup
https://ithelp.ithome.com.tw/upload/images/20211003/20132916IjMjvgd0J7.png

又是個兩層的陣列,這邊一樣是「列」跟「欄」


InlineKeyboardButton

https://core.telegram.org/bots/api#inlinekeyboardbutton
https://ithelp.ithome.com.tw/upload/images/20211003/20132916fPRH3ydru2.png

參數 說明
text 按鈕上的文字
url 按下按鈕會前往的網頁
login_url 登入網站用
callback_data 按下按鈕後會通知機器人
switch_inline_query 切換到其他群組,並且自動產生內聯模式的查詢文字
switch_inline_query_current_chat 在這個群組自動產生內聯模式的查詢文字
callback_game 開啟遊戲用
pay 付款用(有生之年系列)

照著 API 文件打完的程式碼
https://ithelp.ithome.com.tw/upload/images/20211003/20132916OrwuE4jQlT.png
執行之後會像這樣
https://ithelp.ithome.com.tw/upload/images/20211003/20132916HxvkdCAr4i.png


Deep Link

https://core.telegram.org/bots#deep-linking
https://ithelp.ithome.com.tw/upload/images/20211003/20132916iZ97mtsxbS.png
Telegram 有一種特殊的連結,點下去不會開啟網頁,而是直接執行某種功能,常被用在這種按鈕上

使用者相關

tg://user?id={user_id}

這個很特別只有在解析模式(Markdown、HTML)下會有作用,會標記那個 uid 的人,而且點下去會轉到個人資訊頁面,需要機器人見過那個人才有用

機器人相關

https://t.me/{username_bot}?startgroup={payload}
tg://resolve?domain={username_bot}&startgroup={payload}

按下後可以選擇要把機器人加入到哪個群組,payload 會附加在加入群組後 /start 後面,只有機器人看的到

https://t.me/{username_bot}?start={payload}
tg://resolve?domain={username_bot}&start={payload}

這個會導到私訊機器人,按下「開始」後會傳送 /start,然後會附帶 payload

群組相關

https://t.me/joinchat/{chat_token}

群組的邀請連結

https://t.me/c/{chat_id}/{msg_id}?thread={thread_id}
tg://privatepost?channel={chat_id}&post={msg_id}&thread={thread_id}
https://t.me/{chat_username}/{msg_id}?thread={thread_id}
tg://resolve?domain={chat_username}&post={msg_id}&thread={thread_id}

可以用 chat_idchat_username 跳到群組的某一則訊息(chat_id 要去掉 -100),thread 是討論串的意思,要填討論串開始的那則訊息 id,如果填了就會進入那則留言的討論串,如果希望進入討論串能在最下方可以把 msg_id 設成 999999999

https://t.me/c/{chat_id}/{thread_id}?comment={msg_id}
tg://privatepost?channel={chat_id}&post={thread_id}&comment={msg_id}
https://t.me/{chat_username}/{thread_id}?comment={msg_id}
tg://resolve?domain={chat_username}&post={thread_id}&comment={msg_id}

這種方式跟上面填的位置剛好相反,其他部分則相同

設定相關

tg://settings/language

設定語言

tg://settings/folders

設定聊天室分類

tg://settings/devices

查看已登入的裝置

http://t.me/setlanguage/{language_pack}
tg://setlanguage?lang={language_pack}

套用語言包

貼圖、主題相關

https://t.me/addtheme/{theme_pack}
tg://addtheme?slug={theme_pack}

套用主題

https://t.me/addstickers/{sticker_pack}
tg://addstickers?set={sticker_pack}

新增貼圖包

其他

https://t.me/share/url?url={url}&text={text}
tg://msg_url?url={url}&text={text}

選擇聊天室後,會自動將連結和文字瑱入輸入框,常被用在網頁的分享按鈕,可以使用 %0D%0A 換行(等同於 \r\n

更多:https://t.me/s/DeepLink


CallbackQuery

https://core.telegram.org/bots/api#callbackquery
https://ithelp.ithome.com.tw/upload/images/20211003/20132916vLJsLX4KxB.png

參數 說明
id 這個按鈕事件的唯一 id
from 按下按鈕的人
message 有按鈕的那則訊息的詳細資訊,太久遠的訊息可能不會有這個參數
inline_message_id 內聯模式發出去訊息的按鈕才會有這個參數
chat_instance 有按鈕的那則訊息的全域的唯一 id
data 放在按鈕中的資料
game_short_name 遊戲的短名稱,可作為唯一 id

這個是按下按鈕後 TG 伺服器傳送過來的資料格式,昨天已經有加上 print_r($update);,所以直接執行程式就會看到伺服器把按了哪顆按鈕和整個訊息傳了過來
https://ithelp.ithome.com.tw/upload/images/20211003/20132916H3tRZ7ZWTa.png


answerCallbackQuery

https://core.telegram.org/bots/api#answercallbackquery
https://ithelp.ithome.com.tw/upload/images/20211003/20132916M7Laiz5E6M.png

參數 說明
id 指定要回應的 id
text 回應顯示的訊息
show_alert 如果設為 true 就會顯示一個小視窗
url 遊戲用的網址
cache_time 快取時間,在這時間內不會再次通知機器人,而是直接拿剛剛的結果

既然訊息傳了過來,下一步就是要回應訊息

新增一個 elseif,還有處理按鈕的函式 processCallbackQuery()
https://ithelp.ithome.com.tw/upload/images/20211003/20132916V9DNV0Qwnn.png
之後照格式填入,按下執行後按按鈕就能看到效果了
https://ithelp.ithome.com.tw/upload/images/20211003/20132916FhqL41Ynxf.png


上一篇
【PHP Telegram Bot】Day23 - Inline mode(內聯模式):在輸入框使用機器人
下一篇
【PHP Telegram Bot】Day25 - 入群歡迎機器人(1):發送歡迎訊息
系列文
用 PHP 打造專屬於自己的 Telegram 聊天機器人吧!30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言