Ruby bitcoin development tutorial based on mixin network

Time:2019-10-31

Ruby bitcoin development tutorial based on mixin network
Mixin network is a free end-to-end encrypted digital currency trading system.
In this chapter, you can follow the tutorial to create a BOT in mixin messenger to receive user messages and learn how to transfer the robotBitcoinOr let the robot turn you around.Bitcoin.

Mixin network resource summary

Course introduction

  1. Create a robot

Create a robot to receive messages

Through this tutorial, you will learn how to use ruby to create a robot app that can accept messages.

Ruby environment installation:

The program of this tutorial is based onRubyDevelopment.

On macOS

brew update
brew install rbenv
rbenv install 2.6.3
rbenv shell 2.6.3

On Ubuntu

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

Check the installation!

wenewzha:~ wenewzhang$ rbenv shell 2.6.3
wenewzha:minecraft wenewzhang$ ruby -v
ruby 2.6.3p62 (2019-04-16 revision 67580) [x86_64-darwin18]

Install dependency package

This tutorial depends onfaye-websocketAndmixin_bot.

gem install faye-websocket
gem install mixin_bot

Create your project

Go to your working document, create a directory and give it a name, such as:mixin_labs-ruby-bot

mkdir mixin_labs-ruby-bot
mixin_labs-ruby-bot

Create the first robot app

Follow the prompts below to create an apptutorial at mixin. One.

Generate corresponding parameters

Note these generated parameters
They will be used in config.yml.

Ruby bitcoin development tutorial based on mixin network
In the project directory, create config.yml, and replace the generated parameters with yours!

config.yml
`> config.yml

MIXIN_CLIENT_ID: 'a1ce2967-a534-417d-bf12-c86571e4eefa'
MIXIN_CLIENT_SECRET: 'a3f52f6c417f24bfdf583ed884c5d0cb489320c58222b061298e4a2d41a1bbd7'
MIXIN_DEFAULT_SCOPE: 'PROFILE:READ+PHONE:READ+ASSETS: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DhddGo50FzGxYR1LlttdVI
  Q04EytqK7grDDS9PsfeXqdUo0D3NMSJ0BYs/kDsqGSc=
  -----END RSA PRIVATE KEY-----

The parameters to be replaced include: client ID, client secret, and the pin, pin token, session ID, private key.

Classic Hello World

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
}

Compile and run

implementruby websocket_mixin_bot.rbprogram

ruby websocket_mixin_bot.rb

If everything is OK, the display is as follows:

[:open]
[:message]
{"id"=>"2e13092f-4048-488f-82bc-f5ee9f984002", "action"=>"LIST_PENDING_MESSAGES"}

Install mixin messenger on your mobile phone, add a robot as a friend (for example, this robot is 7000101639) and send a message to it. The effect is as follows!
Ruby bitcoin development tutorial based on mixin network

There are many message types supported by mixin messenger. For details, please refer to the following link: websocket message type.

Source code interpretation

After receiving a message, you need to reply a “read” message to the server according to the message [ID], to avoid the server sending the processed message again after the robot logs in again!

ws.send(MixinBot.api.acknowledge_message_receipt(msgid))

complete

Now that your robot app is running, how are you going to transform your robot?

The complete code is here

Next lesson robot receives and processes cryptocurrency

Recommended Today

Python can do anything! Today we can open and run computer software automatically!

Recently, we need to use software automation to do the project. Let’s share it with you!       Let’s take a look at Python operations:   First, how to open the software The first is to use the OS module, which is also the simplest one. If I want to open Notepad, I can: […]