通过本教程,你将学会如何用 Go 创建一个机器人 APP,让它能接受消息.
从 Go 官网下载安装 Go
macOS
下载安装包 go1.12.darwin-amd64.pkg 双击运行,然后按提示安装, 最后将 Go 的 bin 目录加入到$PATH 中.
echo 'export PATH="/usr/local/opt/go/libexec/bin:$PATH"' >> ~/.bash_profile
source ~/.bashrc
如果一切正常,运行 go version就可以看到如下提示了!
go version
go version go1.11.5 darwin/amd64
Ubuntu 18.04
root@n3:/usr/local/bin# snap install go --classic
Ubuntu 16.04
mkdir /usr/local/src
wget https://dl.google.com/go/go1.12.linux-amd64.tar.gz
tar xvf go1.12.linux-amd64.tar.gz
echo 'export PATH=/usr/local/src/go/bin:$PATH' >> ~/.bashrc
root@n3:/usr/local/src# source ~/.bashrc
如果一切正常,运行 go version就可以看到如下提示了!
root@n3:/usr/local/bin# go version
go version go1.12 linux/amd64
强烈推荐为 Go 创建一个工作目录,这让你少了很多关于包的引用的麻烦。 macOS
mkdir ~/workspace/go
echo 'export GOPATH="$HOME/workspace/go"' >> ~/.bash_profile
source ~/.bash_profile
Ubuntu
mkdir ~/workspace/go
echo 'export GOPATH="$HOME/workspace/go"' >> ~/.bashrc
source ~/.bash_profile
root@n3:~# go get github.com/MooooonStar/mixin-sdk-go
package github.com/MooooonStar/mixin-sdk-go: no Go files in /root/workspace/go/src/github.com/MooooonStar/mixin-sdk-go
不用担心 "no Go files" 的提示, ls 可以找到如下目录与文件,事实上,SDK 分别在 messenger,network 中.
ls $GOPATH/src/github.com/MooooonStar/mixin-sdk-go
README.md messenger network
cd ~/workspace/go/src
mkdir mixin_labs-go-bot
cd mixin_labs-go-bot
按下面的提示,到mixin.one创建一个 APPtutorial.
记下这些生成的参数 它们将用于 main.go 中.
main.go
const (
UserId = "21042518-85c7-4903-bb19-f311813d1f51"
PinCode = "911424"
SessionId = "4267b63d-3daa-449e-bc13-970aa0357776"
PinToken = "gUUxpm3fPRVkKZNwA/gk10SHHDtR8LmxO+N6KbsZ/jymmwwVitUHKgLbk1NISdN8jBvsYJgF/5hbkxNnCJER5XAZ0Y35gsAxBOgcFN8otsV6F0FAm5TnWN8YYCqeFnXYJnqmI30IXJTAgMhliLj7iZsvyY/3htaHUUuN5pQ5F5s="
//please delele the blank of PrivateKey the before each line
PrivateKey = `-----BEGIN RSA PRIVATE KEY-----
MIICXQIBAAKBgQCDXiWJRLe9BzPtXmcVe6acaFTY9Ogb4Hc2VHFjKFsp7QRVCytx
3KC/LRojTFViwwExaANTZQ6ectwpAxIvzeYeHDZCXCh6JRFIYK/ZuREmYPcPQEWD
s92Tv/4XTAdTH8l9UJ4VQY4zwqYMak237N9xEvowT0eR8lpeJG0jAjN97QIDAQAB
AoGADvORLB1hGCeQtmxvKRfIr7aEKak+HaYfi1RzD0kRjyUFwDQkPrJQrVGRzwCq
GzJ8mUXwUvaGgmwqOJS75ir2DL8KPz7UfgQnSsHDUwKqUzULgW6nd/3OdDTYWWaN
cDjbkEpsVchOpcdkywvZhhyGXszpM20Vr8emlBcFUOTfpTUCQQDVVjkeMcpRsImV
U3tPYyiuqADhBTcgPBb+Ownk/87jyKF1CZOPvJAebNmpfJP0RMxUVvT4B9/U/yxZ
WNLhLtCXAkEAnaOEuefUxGdE8/55dUTEb7xrr22mNqykJaax3zFK+hSFBrM3gUY5
fEETtHnl4gEdX4jCPybRVc1JSFY/GWoyGwJBAKoLti95JHkErEXYavuWYEEHLNwv
mgcZnoI6cOKVfEVYEEoHvhTeCkoWHVDZOd2EURIQ1eY18JYIZ0M4Z66R8DUCQCsK
iKTR3dA6eiM8qiEQw6nWgniFscpf3PnCx/Iu3U/m5mNr743GhM+eXSj7136b209I
YfEoQiPxRz8O/W+NBV0CQQDVPNqJlFD34MC9aQN42l3NV1hDsl1+nSkWkXSyhhNR
MpobtV1a7IgJGyt5HxBzgNlBNOayICRf0rRjvCdw6aTP
-----END RSA PRIVATE KEY-----`
)
将上面的参数,替换成你在 mixin.one 生成的。
完整而又简洁的代码如下
package main
import (
"context"
"encoding/base64"
"encoding/json"
"log"
"github.com/MooooonStar/mixin-sdk-go/messenger"
mixin "github.com/MooooonStar/mixin-sdk-go/network"
)
type Listener struct {
*messenger.Messenger
}
// interface to implement if you want to handle the message
func (l *Listener) OnMessage(ctx context.Context, msg messenger.MessageView, userId string) error {
data, err := base64.StdEncoding.DecodeString(msg.Data)
if err != nil {
return err
}
if msg.Category == "PLAIN_TEXT" {
log.Printf("I got a message, it said: %s", string(data))
return l.SendPlainText(ctx, msg.ConversationId, msg.UserId, string(data))
} else {
log.Println("Unknown message!", msg.Category)
return err
}
}
const (
UserId = "21042518-85c7-4903-bb19-f311813d1f51"
PinCode = "911424"
SessionId = "4267b63d-3daa-449e-bc13-970aa0357776"
PinToken = "gUUxpm3fPRVkKZNwA/gk10SHHDtR8LmxO+N6KbsZ/jymmwwVitUHKgLbk1NISdN8jBvsYJgF/5hbkxNnCJER5XAZ0Y35gsAxBOgcFN8otsV6F0FAm5TnWN8YYCqeFnXYJnqmI30IXJTAgMhliLj7iZsvyY/3htaHUUuN5pQ5F5s="
//please delele the blank of PrivateKey the before each line
PrivateKey = `-----BEGIN RSA PRIVATE KEY-----
MIICXQIBAAKBgQCDXiWJRLe9BzPtXmcVe6acaFTY9Ogb4Hc2VHFjKFsp7QRVCytx
3KC/LRojTFViwwExaANTZQ6ectwpAxIvzeYeHDZCXCh6JRFIYK/ZuREmYPcPQEWD
s92Tv/4XTAdTH8l9UJ4VQY4zwqYMak237N9xEvowT0eR8lpeJG0jAjN97QIDAQAB
AoGADvORLB1hGCeQtmxvKRfIr7aEKak+HaYfi1RzD0kRjyUFwDQkPrJQrVGRzwCq
GzJ8mUXwUvaGgmwqOJS75ir2DL8KPz7UfgQnSsHDUwKqUzULgW6nd/3OdDTYWWaN
cDjbkEpsVchOpcdkywvZhhyGXszpM20Vr8emlBcFUOTfpTUCQQDVVjkeMcpRsImV
U3tPYyiuqADhBTcgPBb+Ownk/87jyKF1CZOPvJAebNmpfJP0RMxUVvT4B9/U/yxZ
WNLhLtCXAkEAnaOEuefUxGdE8/55dUTEb7xrr22mNqykJaax3zFK+hSFBrM3gUY5
fEETtHnl4gEdX4jCPybRVc1JSFY/GWoyGwJBAKoLti95JHkErEXYavuWYEEHLNwv
mgcZnoI6cOKVfEVYEEoHvhTeCkoWHVDZOd2EURIQ1eY18JYIZ0M4Z66R8DUCQCsK
iKTR3dA6eiM8qiEQw6nWgniFscpf3PnCx/Iu3U/m5mNr743GhM+eXSj7136b209I
YfEoQiPxRz8O/W+NBV0CQQDVPNqJlFD34MC9aQN42l3NV1hDsl1+nSkWkXSyhhNR
MpobtV1a7IgJGyt5HxBzgNlBNOayICRf0rRjvCdw6aTP
-----END RSA PRIVATE KEY-----`
)
func main() {
ctx := context.Background()
m := messenger.NewMessenger(UserId, SessionId, PrivateKey)
l := &Listener{m}
go m.Run(ctx, l)
select {}
}
执行 go build 将创建一个 mixin_labs-go-bot,然后执行
cd mixin_labs-go-bot
go build
./mixin_labs-go-bot
在手机安装 Mixin Messenger,增加机器人为好友,(比如这个机器人是 7000101639) 然后发送消息给它,效果如下!
WebSocket 是建立在 TCP 基础之上的全双工通讯方式,连接到 Mixin Network 并发送"LISTPENDINGMESSAGES"消息,服务器以后会将收到的消息转发给此程序!
ctx := context.Background()
m := messenger.NewMessenger(UserId, SessionId, PrivateKey)
l := &Listener{m}
go m.Run(ctx, l)
当服务器给机器人推送消息的时候,机器人会原封不动的回复回去.
func (l *Listener) OnMessage(ctx context.Context, msg messenger.MessageView, userId string) error {
data, err := base64.StdEncoding.DecodeString(msg.Data)
if err != nil {
return err
}
if msg.Category == "PLAIN_TEXT" {
log.Printf("I got a message, it said: %s", string(data))
return l.SendPlainText(ctx, msg.ConversationId, msg.UserId, string(data))
} else {
log.Println("Unknown message!", msg.Category)
return err
}
}
Mixin Messenger 支持的消息类型很多,除了文本,还有图片,视频,语音等等,具体可到下面链接查看: WebSocket 消息类型.
现在你的机器人 APP 运行起来了,你打算如何改造你的机器人呢?
完整的代码在这儿
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.