在昨天講解了指令集後
今天來講解Bot的回傳方式
這是最基本也是最基礎的純文字
在前幾天的程式碼裡
msg := tgbotapi.NewMessage(update.Message.Chat.ID, update.Message.Text)
當中第二個參數就是回傳的文字
第二種是下方選單的按鈕清單
先建一組按鈕清單
var command = tgbotapi.NewReplyKeyboard(
tgbotapi.NewKeyboardButtonRow(
tgbotapi.NewKeyboardButton("我的訂單"),
tgbotapi.NewKeyboardButton("團購清單"),
),
)
在msg新增 ReplyMarkup = command
即可
for update := range updates {
if update.Message == nil { // ignore any non-Message Updates
continue
}
log.Printf("[%s] %s", update.Message.From.UserName, update.Message.Text)
msg := tgbotapi.NewMessage(update.Message.Chat.ID, "按鈕清單")
msg.ReplyToMessageID = update.Message.MessageID
msg.ReplyMarkup = command
bot.Send(msg)
}
Inline keyboards 有三種模式
這裡只用到Data的部分當作回傳的資料內容
一樣先建立按鈕清單
var store = tgbotapi.NewInlineKeyboardMarkup(
tgbotapi.NewInlineKeyboardRow(
tgbotapi.NewInlineKeyboardButtonData("50嵐", "50嵐"),
tgbotapi.NewInlineKeyboardButtonData("CoCo", "CoCo"),
),
)
針對上面的Keyboard按鈕進行事件處理
switch update.Message.Text {
case "我的訂單":
returntext = "還沒實作"
msg := tgbotapi.NewMessage(update.Message.Chat.ID, returntext)
msg.ReplyMarkup = command
bot.Send(msg)
case "團購清單":
returntext = "店家資訊"
msg := tgbotapi.NewMessage(update.Message.Chat.ID, returntext)
msg.ReplyMarkup = store
bot.Send(msg)
default:
msg := tgbotapi.NewMessage(update.Message.Chat.ID, update.Message.Text)
bot.Send(msg)
}
針對InlineKeyboard按鈕進行處理
if update.CallbackQuery != nil {
fmt.Print(update)
bot.AnswerCallbackQuery(tgbotapi.NewCallback(update.CallbackQuery.ID, update.CallbackQuery.Data))
bot.Send(tgbotapi.NewMessage(update.CallbackQuery.Message.Chat.ID, update.CallbackQuery.Data))
}
結果的Code
package main
import (
"fmt"
"log"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
)
var command = tgbotapi.NewReplyKeyboard(
tgbotapi.NewKeyboardButtonRow(
tgbotapi.NewKeyboardButton("我的訂單"),
tgbotapi.NewKeyboardButton("團購清單"),
),
)
var store = tgbotapi.NewInlineKeyboardMarkup(
tgbotapi.NewInlineKeyboardRow(
tgbotapi.NewInlineKeyboardButtonData("50嵐", "50嵐"),
tgbotapi.NewInlineKeyboardButtonData("CoCo", "CoCo"),
),
)
func main() {
bot, err := tgbotapi.NewBotAPI("Token")
if err != nil {
log.Panic(err)
}
bot.Debug = true
log.Printf("Authorized on account %s", bot.Self.UserName)
u := tgbotapi.NewUpdate(0)
u.Timeout = 60
updates, err := bot.GetUpdatesChan(u)
returntext := ""
for update := range updates {
if update.CallbackQuery != nil {
fmt.Print(update)
bot.AnswerCallbackQuery(tgbotapi.NewCallback(update.CallbackQuery.ID, update.CallbackQuery.Data))
bot.Send(tgbotapi.NewMessage(update.CallbackQuery.Message.Chat.ID, update.CallbackQuery.Data))
}
if update.Message == nil { // ignore any non-Message Updates
continue
}
log.Printf("[%s] %s", update.Message.From.UserName, update.Message.Text)
switch update.Message.Text {
case "我的訂單":
returntext = "還沒實作"
msg := tgbotapi.NewMessage(update.Message.Chat.ID, returntext)
msg.ReplyMarkup = command
bot.Send(msg)
case "團購清單":
returntext = "店家資訊"
msg := tgbotapi.NewMessage(update.Message.Chat.ID, returntext)
msg.ReplyMarkup = store
bot.Send(msg)
default:
msg := tgbotapi.NewMessage(update.Message.Chat.ID, update.Message.Text)
bot.Send(msg)
}
}
}
這樣就建立了一個超級簡單的對話機器人了
後面只要再針對他收到的資料進行處理
跟後端的系統串接 就可以開始準備使用機器人來做點餐的動作了