[Day 23] Mattermost - ChatOps

ChatOps with Mattermost

chatops好处

  • 成本低
  • 互动性佳
  • 即时反应提高效率
  • 讯息公开透明化
  • 提升团队维运经验
    • 因为是公开讯息所以大家可以在窗口很透明的看到操作,以学习经验

chatops成本

聊天机器人开发、维护


chatops实作

  • 监听
  • 确认指令
  • 发动

git clone 范例专案

git clone https://github.com/mattermost/mattermost-bot-sample-golang
cd mattermost-bot-sample-golang 

建立一个使用者给bot使用

然後把此帐号加入到设定好的要监听的channel内

设定

然後点开bot_sample.go查看

调整const内容

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"
)

调整http连线设定

func main() {
	println(SAMPLE_NAME)

	SetupGracefulShutdown()

	client = model.NewAPIv4Client("http://localhost:8065")

调整WS连线设定

调整第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)
	}

查看触发command的单字

看一下

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

与BOT互动

https://ithelp.ithome.com.tw/upload/images/20210927/2011528971JuKWhw6y.png


新增互动指令

我们练习一下加个把当下目录列出来的指令

把原本执行的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

https://ithelp.ithome.com.tw/upload/images/20210929/20115289LnJGASrKWw.png

这样就完成简单的chatops小小范例罗,之後看你们要加什麽上去都可以 :)


<<:  [Day30] Tableau 轻松学 - 总结

>>:  资料库练习

生成模式 - abstract factory

首先从生成模式开始,第一种生成模式是 abstract factory (抽象工厂) 抽象工厂的目的...

Day 12. Zabbix 样板套用功能

安装的部分都介绍完了,有一个部分大家可能有些困惑,在新增主机 Host 的时候为什麽要加 Templ...

铁人赛 Day13-- MySQL函式

mysqli_num_rows() 取得查询笔数 可以使用 mysqli_num_rows() 函式...

D17 - 彭彭的课程# Python Module 模组的载入与使用(2)

今天波金钟奖大家有没有收看 现在很多明星我都认不出来了QQ 时代的眼泪阿 好的今天紧接昨天的第二部份...

BPMN 业务流程图

BPMN (Business Process Model and Notation) 也是一个用来做...