在昨天讲解了指令集後
今天来讲解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)
}
}
}
这样就建立了一个超级简单的对话机器人了
後面只要再针对他收到的资料进行处理
跟後端的系统串接 就可以开始准备使用机器人来做点餐的动作了
<<: Quora、Answer the Public: 解决用户问题,先知道大家都问些什麽问题?
物联网(IoT)的重大发展促进了日常生活中连网设备的激增。 从智能家庭到自动化医疗保健应用,物联网无...
严格模式可以想成是React的debug工具,严格模式不会渲染出任何的UI元件,它会检查其包覆下所有...
D3js Diagram常见的两点浪漫路径 用途 在绘制diagram图表时,会用到的垂直水平连线,...
环境安装 在使用Python开发AI时,由於需时时查看处理中的训练资料,於是大多使用Jupyter ...
jcmd介绍 jcmd是在JDK1.7之後新增的一项工具。它是一个多功能的工具,就想把瑞士刀一样,集...