iT邦幫忙

2021 iThome 鐵人賽

DAY 23
0
Software Development

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

【PHP Telegram Bot】Day23 - Inline mode(內聯模式):在輸入框使用機器人

https://ithelp.ithome.com.tw/upload/images/20211002/20132916VYIWNTI1lX.png

雖然還有另一個按紐,但是今天我想先來玩玩內聯模式

前置作業

還記得 Day04 - Telegram 機器人的設定 文章中間有看到過的那個 Inline mode 嗎,這個設定在 Bot Settings 中,找到它並點進去
https://ithelp.ithome.com.tw/upload/images/20211002/20132916mI3OEh3Jq3.png
然後按下 Turn inline mode on,開啟後會像底下這樣:
https://ithelp.ithome.com.tw/upload/images/20211002/201329161C6HKJAtAv.png
中間那顆 Edit inline placeholder 是設定顯示的浮水印文字

按下按鈕後,它會請你輸入一段文字用來提示使用者
https://ithelp.ithome.com.tw/upload/images/20211002/20132916njrIN1vfq5.png
https://ithelp.ithome.com.tw/upload/images/20211002/20132916OkC3tThdir.png
另外還會多兩個設定,點一下 Back to Settings,你會發現多了兩個按紐
https://ithelp.ithome.com.tw/upload/images/20211002/201329161MkDcRZmuL.png

Inline Location Data

開啟的話,就會在使用者查詢時傳送他的位置,如果你是要做與位置相關的查詢功能的話可以開啟。其他的就不建議了,因為使用者不一定願意提供位置給你,開了反而會增加使用的門檻
https://ithelp.ithome.com.tw/upload/images/20211002/20132916xn15ZxscAU.png

Inline Feedback

這個是設定有多少機率發送使用者的選擇訊息給機器人,機率越高機器人要處理的訊息就越多,如果沒有需要知道使用者按了哪個選項就設成 0% 吧
https://ithelp.ithome.com.tw/upload/images/20211002/20132916XV6O4voKXs.png
https://ithelp.ithome.com.tw/upload/images/20211002/20132916L1G62Z79s6.png


Inline mode

https://core.telegram.org/bots/api#inline-mode
https://ithelp.ithome.com.tw/upload/images/20211002/20132916rQPPre9wpi.png
恩恩,這個我們剛剛設定好了


InlineQuery

https://core.telegram.org/bots/api#inlinequery
https://ithelp.ithome.com.tw/upload/images/20211002/20132916veVtRNMqz2.png
這個是從 TG 伺服器傳過來的訊息格式

參數 說明
id 這個查詢的唯一 id
from 使用者的資訊
query 輸入的文字
offset 查詢的頁數
chat_type 聊天室的類型
location 使用者的位置

我們來看看是不是真的長這樣,先打開你的程式,然後在這中間加上 print_r($update);,把伺服器傳過來的訊息印出來
https://ithelp.ithome.com.tw/upload/images/20211002/20132916QZaxQyjipk.png
按下執行後,在隨便一個聊天室中打上你機器人的 username
https://ithelp.ithome.com.tw/upload/images/20211002/20132916WahrH4hyzg.png
之後你就會看到終端機出現了一坨訊息,確實跟官方寫得差不多,不過缺少了 chat_type
https://ithelp.ithome.com.tw/upload/images/20211002/20132916ewNzJHrCaz.png
這坨錯誤訊息是因為現在 $update 陣列裡面沒有叫做 message 的東東,只要在處理訊息的函式外面再加一個 if 就行了,如果這個東東存在我才執行這段程式
https://ithelp.ithome.com.tw/upload/images/20211002/20132916FOKk3pHGVv.png
https://ithelp.ithome.com.tw/upload/images/20211002/20132916R23OtLH0yk.png
另外我們要來處理 inline_query,所以要再加個 elseif,還有一個新的函式 processInlineQuery()
https://ithelp.ithome.com.tw/upload/images/20211002/20132916Nxz0wmS8Vl.png


answerInlineQuery

https://core.telegram.org/bots/api#answerinlinequery
https://ithelp.ithome.com.tw/upload/images/20211002/20132916nVDP7UeXzP.png
這個是要回應給伺服器的格式

參數 說明
inline_query_id 指定要回應的 id
results 查詢的結果放在這裡
cache_time 快取時間,在這時間內不會再次查詢不會通知機器人,而是直接拿剛剛的結果
is_personal 如果設為 true,快取就會是相對於個人的,而不同人輸入相同訊息時也會通知機器人
next_offset 下一頁的標記。如果查詢結果很多,使用者往下滑時,伺服器就會以這個 offset 再通知機器人。如果這是最後一頁的話就填空字串
switch_pm_text 顯示在列表最上方的文字,點下後會轉到私訊機器人,並且發送 /start
switch_pm_parameter 接在 /start 後的參數,只有機器人看的到

我們要在函式裡回應伺服器,告訴它查詢的結果,先試試回應沒有任何查詢結果,記得打完這段先在按 Ctrl + C 終止程式,重新執行
https://ithelp.ithome.com.tw/upload/images/20211002/20132916URVR4z1wQm.png
再次在輸入框使用機器人,就會看到剛剛設定的回應
https://ithelp.ithome.com.tw/upload/images/20211002/20132916MhTQCerfg6.png


InlineQueryResult

https://core.telegram.org/bots/api#inlinequeryresult
https://ithelp.ithome.com.tw/upload/images/20211002/201329168Wv6poXvAP.png
查詢的結果有多達 20 種的格式可以選擇,我就示範一種就好了


InlineQueryResultArticle

https://core.telegram.org/bots/api#inlinequeryresultarticle
https://ithelp.ithome.com.tw/upload/images/20211002/20132916S7oYLLgKrN.png
以文章的的格式來填查詢結果

參數 說明
type 這個陣列內容的種類,必須填 article
id 這個查詢結果的唯一 id
title 文章的標題
input_message_content 按下選項後會發送的文字
reply_markup 發送文字下方的按紐,這個明天會講
url 文章的連結
hide_url 設定為 true 會在查詢結果隱藏連結
description 文章的說明
thumb_url 文章縮圖的 url
thumb_width 圖片的寬度
thumb_height 圖片的高度

把這個東東填進 results 後就會像這樣子:
https://ithelp.ithome.com.tw/upload/images/20211002/20132916F8Jwad0juC.png
查詢時顯示的樣子:
https://ithelp.ithome.com.tw/upload/images/20211002/20132916vWOAz8EatW.png
如果有好幾個查詢結果的話就寫成這樣,記得 id 是唯一的,每個查詢結果都不一樣不可以重複
https://ithelp.ithome.com.tw/upload/images/20211002/20132916kODJtpKhP5.png
重新執行程式再查詢就會變成一長串
https://ithelp.ithome.com.tw/upload/images/20211002/201329164NwcMvLTPF.png


上一篇
【PHP Telegram Bot】Day22 - ReplyKeyboardMarkup:讓輸入框下方出現按鈕區域
下一篇
【PHP Telegram Bot】Day24 - InlineKeyboardMarkup、Deep Link:來玩玩訊息下方的按鈕吧
系列文
用 PHP 打造專屬於自己的 Telegram 聊天機器人吧!30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言