Mixin Network 是一個免費的 極速的端對端加密數字貨幣交易系統.
在本章中,你能夠按教程在Mixin Messenger中建立一個bot來接收用戶消息, 學到如何給機器人轉比特幣 或者 讓機器人給你轉比特幣.php
經過本教程,你將學會如何用Ruby建立一個機器人APP,讓它能接受消息.github
本教程的程序基於 Ruby 開發.web
On macOSshell
brew update brew install rbenv rbenv install 2.6.3 rbenv shell 2.6.3
On Ubuntujson
sudo apt update sudo apt install git curl libssl-dev libreadline-dev zlib1g-dev autoconf \ bison build-essential libyaml-dev libreadline-dev libncurses5-dev libffi-dev libgdbm-dev curl -sL https://github.com/rbenv/rbenv-installer/raw/master/bin/rbenv-installer | bash - echo 'export PATH=/root/.rbenv/bin:$PATH' >> ~/.bashrc echo 'eval "$(rbenv init -)"' >> ~/.bashrc source ~/.bashrc rbenv install 2.6.3 rbenv shell 2.6.3
檢查安裝狀況!api
wenewzha:~ wenewzhang$ rbenv shell 2.6.3 wenewzha:minecraft wenewzhang$ ruby -v ruby 2.6.3p62 (2019-04-16 revision 67580) [x86_64-darwin18]
本教程依賴 faye-websocket 與 mixin_bot.ruby
gem install faye-websocket gem install mixin_bot
到你的工做文檔中,建立一個目錄,並取一個名字,好比:mixin_labs-ruby-botbash
mkdir mixin_labs-ruby-bot mixin_labs-ruby-bot
按下面的提示,到mixin.one建立一個APPtutorial.服務器
記下這些生成的參數
它們將用於config.yml中.
在項目目錄下,建立config.yml,將生成的參數,替換成你的!
config.yml
`> config.yml
MIXIN_CLIENT_ID: 'a1ce2967-a534-417d-bf12-c86571e4eefa' MIXIN_CLIENT_SECRET: 'a3f52f6c417f24bfdf583ed884c5d0cb489320c58222b061298e4a2d41a1bbd7' MIXIN_DEFAULT_SCOPE: 'PROFILE:READ+PHONE:READ+ASSETS:READ' MIXIN_PIN_CODE: '457965' MIXIN_SESSION_ID: '26ed1f52-a3b4-4cc3-840f-469d3f19b10b' MIXIN_PIN_TOKEN: '0t4EG7tJerZYds7N9QS0mlRPCYsEVTQBe9iD1zNBCFN/XO7XEB87ypsCDWfRmDiZ7izzB/nokuMJEu6RJShMHCdIwYISU9xckA/8hIsRVydvoP14G/9kRidMHl/3RPLDMK6U2yCefo2BH0kQdbcRDxpiddqrMc4fYmZo6UddU/A=' MIXIN_PRIVATE_KEY: | -----BEGIN RSA PRIVATE KEY----- MIICXAIBAAKBgQDaSPE8Cu18xzr8MOcgJx8tQnRdlS7c6JVs23497IGdIybIUYmZ 8zvgrFozpGjQYz2ayRDMWUQd/wm7e0Tf7n4bVCmQfkk72usAHX6pNA4HUeTeTmDT sZQKdVx0K84Y3u512cAi5artnUjIsFRPP/LhAX0ujdgNMWIcHrMRh77s1wIDAQAB AoGAVPW3Dwuhy8MvriDKlLUlaVRIPnRmPQ05u5ji1e9Ls4GPAsDZsdX+JEBxC1Ce ix1VSP2hUCgeXx55B0O/VvlYk0pfogrxDgOw2dP04uboMG7tSE4TZK8J9zFPUrE0 wizFmbkgV2OEw33r00FqEhr0KnB9kXOzB5BvKN/FVyXui+ECQQDz1x3hOypW2kM9 uOqjQyg55VDkkXVZ8RgOmVd24MfkDjRauj1oGgLUWvINzhmXN5m84IhlOz1hgEuO enHOpMmDAkEA5SuVeRhBZofUoaRbFxWL4jAN6+uuxFxZ0gCc9l4gwFkQp0RbEw/S tiX9Cl06JR2oc2FBlaO5Vi1u8XfxOSUzHQJBANijfKaJHFrB3A/QZJbcqbaWaEJK gYqBSzBdSHoTx0R04krhQIFm6rCkhH2DaPUSrwJCMqxN74DarUZOvyIrAeUCQH2F ecFx/6BhFZ3Tn/Ds5ElneLiXxonW63uSymZG+DlijzSOxDOUnx0VgZuDpK1fqTxJ MNr9ai5BhFrOD1n1fiECQBafDxsfFQv3w6j5/2PL54DhddGo50FzGxYR1LlttdVI Q04EytqK7grDDS9PsfeXqdUo0D3NMSJ0BYs/kDsqGSc= -----END RSA PRIVATE KEY-----
須要替換的參數包括: client_id, client_secret, and the pin, pin token, session_id, private key.
require 'faye/websocket' require 'eventmachine' require 'json' require 'zlib' require 'mixin_bot' require 'yaml' yaml_hash = YAML.load_file('./config.yml') MixinBot.client_id = yaml_hash["MIXIN_CLIENT_ID"] MixinBot.session_id = yaml_hash["MIXIN_SESSION_ID"] MixinBot.client_secret = yaml_hash["MIXIN_CLIENT_SECRET"] MixinBot.pin_token = yaml_hash["MIXIN_PIN_TOKEN"] MixinBot.private_key = yaml_hash["MIXIN_PRIVATE_KEY"] access_token = MixinBot.api.access_token("GET","/","") puts access_token authorization = format('Bearer %s', access_token) EM.run { ws = Faye::WebSocket::Client.new('wss://blaze.mixin.one/', ["Mixin-Blaze-1"], :headers => { 'Authorization' => authorization } ) ws.on :open do |event| p [:open] ws.send(MixinBot.api.list_pending_message) end ws.on :message do |event| p [:message] data = event.data msg = MixinBot.api.read_message(data) jsmsg = JSON.parse msg # p jsmsg # p jsmsg["data"] if jsmsg["action"] == "CREATE_MESSAGE" && jsmsg["data"] != nil msgid = jsmsg["data"]["message_id"] ws.send(MixinBot.api.acknowledge_message_receipt(msgid)) if jsmsg["data"]["category"] == "PLAIN_TEXT" conversation_id = jsmsg["data"]["conversation_id"] decoded_msg = Base64.decode64 jsmsg["data"]["data"] p decoded_msg reply_msg = MixinBot.api.plain_text_message(conversation_id, decoded_msg) ws.send(reply_msg) end if jsmsg["data"]["category"] == "SYSTEM_ACCOUNT_SNAPSHOT" jsdata = JSON.parse (Base64.decode64(jsmsg["data"]["data"])) p jsdata end end end ws.on :error do |event| p [:error] end ws.on :close do |event| p [:close, event.code, event.reason] ws = nil end }
執行 ruby websocket_mixin_bot.rb 程序
ruby websocket_mixin_bot.rb
若是一切正常,顯示以下:
[:open] [:message] {"id"=>"2e13092f-4048-488f-82bc-f5ee9f984002", "action"=>"LIST_PENDING_MESSAGES"}
在手機安裝 Mixin Messenger,增長機器人爲好友,(好比這個機器人是7000101639) 而後發送消息給它,效果以下!
Mixin Messenger支持的消息類型不少,具體可到下面連接查看: WebSocket消息類型.
每接收到一個消息,須要按消息編號(message_id)給服務器回覆一個"已讀"的消息,避免服務器在機器人從新登入後,再次發送處理過的消息!
ws.send(MixinBot.api.acknowledge_message_receipt(msgid))
如今你的機器人APP運行起來了,你打算如何改造你的機器人呢?
完整的代碼在這兒