Mixin Network 是一個免費的 極速的端對端加密數字貨幣交易系統. 在本章中,你可以按教程在Mixin Messenger中建立一個bot來接收使用者訊息, 學到如何給機器人轉比特幣 或者 讓機器人給你轉比特幣.
課程簡介
建立一個接受訊息的機器人
通過本教程,你將學會如何用Go建立一個機器人APP,讓它能接受訊息.
Go 1.12 的安裝:
從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的工作目錄
強烈推薦為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
複製程式碼
安裝 Mixin Network SDK for Go
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
複製程式碼
在GOPATH下建立專案目錄
cd ~/workspace/go/src
mkdir mixin_labs-go-bot
cd mixin_labs-go-bot
複製程式碼
你好,世界!
建立第一個機器人APP
按下面的提示,到mixin.one建立一個APPtutorial.
生成相應的引數
記下這些生成的引數 它們將用於main.go中.
在專案目錄下,建立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執行起來了,你打算如何改造你的機器人呢?
完整的程式碼在這兒