我正在Tego上写一个机器人,我不知道如何在机器人中实现有限状态机.在此之前,我用内置了FSM的python aigraph编写了机器人.在golang 怎么做呢?
package main
import (
"fmt"
"os"
"github.com/mymmrac/telego"
th "github.com/mymmrac/telego/telegohandler"
tu "github.com/mymmrac/telego/telegoutil"
)
func main() {
botToken := os.Getenv("TOKEN")
// Note: Please keep in mind that default logger may expose sensitive information,
// use in development only
bot, err := telego.NewBot(botToken, telego.WithDefaultDebugLogger())
if err != nil {
fmt.Println(err)
os.Exit(1)
}
// Get updates channel
updates, _ := bot.UpdatesViaLongPolling(nil)
// Create bot handler and specify from where to get updates
bh, _ := th.NewBotHandler(bot, updates)
// Stop handling updates
defer bh.Stop()
// Stop getting updates
defer bot.StopLongPolling()
// Register new handler with match on command `/start`
bh.Handle(func(bot *telego.Bot, update telego.Update) {
// Send message
_, _ = bot.SendMessage(tu.Message(
tu.ID(update.Message.Chat.ID),
fmt.Sprintf("Hello %s!", update.Message.From.FirstName),
))
}, th.CommandEqual("start"))
// Register new handler with match on any command
// Handlers will match only once and in order of registration,
// so this handler will be called on any command except `/start` command
bh.Handle(func(bot *telego.Bot, update telego.Update) {
// Send message
_, _ = bot.SendMessage(tu.Message(
tu.ID(update.Message.Chat.ID),
"Unknown command, use /start",
))
}, th.AnyCommand())
// Start handling updates
bh.Start()
}
此代码摘自官方文档https://github.com/mymmrac/telego
我用谷歌搜索了一下,没有找到任何对我有帮助的东西.