聊天机器人开发、维护
git clone https://github.com/mattermost/mattermost-bot-sample-golang
cd mattermost-bot-sample-golang
然後把此帐号加入到设定好的要监听的channel内
然後点开bot_sample.go查看
const (
SAMPLE_NAME = "Mattermost Bot Sample"
USER_EMAIL = "[email protected]"
USER_PASSWORD = "ithomebot"
USER_NAME = "ithome-bot"
USER_FIRST = "Sample"
USER_LAST = "Bot"
TEAM_NAME = "rainforest"
CHANNEL_LOG_NAME = "gitea"
)
func main() {
println(SAMPLE_NAME)
SetupGracefulShutdown()
client = model.NewAPIv4Client("http://localhost:8065")
调整第67行 ws参数将 ws://localhost:8065
改为你的URL
// Lets start listening to some channels via the websocket!
webSocketClient, err := model.NewWebSocketClient4("ws://localhost:8065", client.AuthToken)
if err != nil {
println("We failed to connect to the web socket")
PrintError(err)
}
看一下
func HandleMsgFromDebuggingChannel(){}
// if you see any word matching 'alive' then respond
if matched, _ := regexp.MatchString(`(?:^|\W)alive(?:$|\W)`, post.Message); matched {
SendMsgToDebuggingChannel("Yes I'm running", post.Id)
return
}
// if you see any word matching 'up' then respond
if matched, _ := regexp.MatchString(`(?:^|\W)up(?:$|\W)`, post.Message); matched {
SendMsgToDebuggingChannel("Yes I'm running", post.Id)
return
}
// if you see any word matching 'running' then respond
if matched, _ := regexp.MatchString(`(?:^|\W)running(?:$|\W)`, post.Message); matched {
SendMsgToDebuggingChannel("Yes I'm running", post.Id)
return
}
// if you see any word matching 'hello' then respond
if matched, _ := regexp.MatchString(`(?:^|\W)hello(?:$|\W)`, post.Message); matched {
SendMsgToDebuggingChannel("Yes I'm running", post.Id)
return
}
所以此机器人当收到「alive, up, running, hello」时,会有互动。
go run *.go
启动後会在Terminal上显示
Ithome Mattermost Bot
Server detected and is running version 5.32.0
把原本执行的go run中断掉
至
func HandleMsgFromDebuggingChannel(){}
下面新增 ls 指令列出该目录档案
if matched, _ := regexp.MatchString(`(?:^|\W)ithome(?:$|\W)`, post.Message); matched {
cmd := exec.Command("ls")
out, err := cmd.CombinedOutput()
if err != nil {
log.Fatalf("cmd.Run() failed with %s\n", err)
}
SendMsgToDebuggingChannel(string(out), post.Id)
return
}
储存後,再下一次指令
go run *.go
这样就完成简单的chatops小小范例罗,之後看你们要加什麽上去都可以 :)
首先从生成模式开始,第一种生成模式是 abstract factory (抽象工厂) 抽象工厂的目的...
安装的部分都介绍完了,有一个部分大家可能有些困惑,在新增主机 Host 的时候为什麽要加 Templ...
mysqli_num_rows() 取得查询笔数 可以使用 mysqli_num_rows() 函式...
今天波金钟奖大家有没有收看 现在很多明星我都认不出来了QQ 时代的眼泪阿 好的今天紧接昨天的第二部份...
BPMN (Business Process Model and Notation) 也是一个用来做...