【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


<<:  Day_21 : 让 Vite 来开启你的Vue 之 跨元件资料传递 Provide & Inject

>>:  [Day 28] Leetcode 78. Subsets (C++)

Day 25 「行礼如仪?行将就木?」Service 与单元测试(下)

笔者写作年资不算长,但写到後来,还是多多少少能在动笔之前,感受一些主题的容易度,譬如理论的主题,对我...

UML 图摘要

UML (统一建模语言),对於所有学过 OOP 语言的人是一个耳熟能详的图表,UML, SysML,...

How to Reset forgotten root password in RHEL/CentOS 7/8

As a Linux Administrator, You must know the import...

参赛心得&感想

首先要先回顾、检讨一下, 刚好遇到专案忙碌的时候, 写出来的文章有点惨不忍睹, 很多程序的细节其实没...

[Day 27] 建立table连线控制

建好table後,再来就是要建立Controller建立路径来控制更新、删除、取得资料的动作。 在终...