Skip to content

an-lee/mixin_bot

Repository files navigation

MixinBot

CI

An API wrapper for Mixin Network

Installation

Add to gemfile, and bundle install

gem 'mixin_bot'

Or

gem install mixin_bot

Usage

CLI

Commands:
  mixinbot api PATH -k, --keystore=KEYSTORE                                   # request PATH of Mixin API
  mixinbot decodetx TRANSACTION                                               # decode raw transaction
  mixinbot encrypt PIN -k, --keystore=KEYSTORE                                # encrypt PIN using private key
  mixinbot generatetrace HASH                                                 # generate trace ID from Tx hash
  mixinbot help [COMMAND]                                                     # Describe available commands or one specific command
  mixinbot nftmemo -c, --collection=COLLECTION -h, --hash=HASH -t, --token=N  # memo for mint NFT
  mixinbot unique UUIDS                                                       # generate unique UUID for two or more UUIDs
  mixinbot version                                                            # Distay MixinBot version

Options:
  -a, [--apihost=APIHOST]        # Specify mixin api host
                                 # Default: api.mixin.one
  -r, [--pretty], [--no-pretty]  # Print output in pretty
                                 # Default: true

Example:

$ mixinbot api /me -k ~/.mixinbot/keystore.json

Initialize params

To use MixinBot api, you should set the keys first.

MixinBot.configure do
  app_id = '25696f85-b7b4-4509-8c3f-2684a8fc4a2a'
  client_secret = 'd9dc58107bacde671...'
  session_id ='25696f85-b7b4-4509-8c3f-2684a8fc4a2a'
  server_public_key = 'b0pjBUKI0Vp9K+NspaL....'
  session_private_key = '...'
end

Call mixin apis

Then you can use MixinBot by call MixinBot.api, for example

# get the bot profile
MixinBot.api.me

# get assets of the bot
MixinBot.api.assets

# transfer asset to somebody
MixinBot.api
  .create_transfer(
    '123456', # pin_code
    asset_id: '965e5c6e-434c-3fa9-b780-c50f43cd955c', # the asset uuid to transfer
    opponent_id: '6ae1c7ae-1df1-498e-8f21-d48cb6d129b5', # receiver's mixin uuid
    amount: 0.00000001, # amount
    memo: 'test from MixinBot', # memo, 140 length at max
    trace_id: '0798327a-d499-416e-9b26-5cdc5b7d841e' # a uuid to trace transfer
)

# etc

Connect Mixin Blaze

Your bot can receive/send messages from/to any users in Mixin Network, including all users in Mixin Messenger by connecting to Mixin Blaze.

With MixinBot, doing this is super easy.

# run it in a EventMachine
EM.run {
  MixinBot.api.start_blaze_connect do
    # do something when the websocket connected
    def on_open(blaze, _event)
      p [Time.now.to_s, :on_open]

      # send the list_pending_message to receive messages
      blaze.send list_pending_message
    end

    # do something when receive message
    def on_message(blaze, event)
      raw = JSON.parse ws_message(event.data)
      p [Time.now.to_s, :on_message, raw&.[]('action')]

      blaze.send acknowledge_message_receipt(raw['data']['message_id']) unless raw&.[]('data')&.[]('message_id').nil?
    end

    # do something when websocket error
    def on_error(blaze, event)
      p [Time.now.to_s, :on_error]
    end

    # do something when websocket close
    def on_close(blaze, event)
      p [Time.now.to_s, :on_close, event.code, event.reason]
    end
  end
}

Multiple Bot management

If you need to manage multiple mixin bot, you can config like this.

bot1_api = MixinBot::API.new(
  app_id: '...',
  client_secret: '...',
  session_id: '...',
  server_public_key: '...',
  session_private_key: '...'
)

bot2_api = MixinBot::API.new(
  app_id: '...',
  client_secret: '...',
  session_id: '...',
  server_public_key: '...',
  session_private_key: '...'
)

bot1_api.me
bot2_api.me

More Example

See in the Spec files.

For WebSocket use case, see in examples/blaze.rb.

Test

Clone the project:

git clone https://github.com/an-lee/mixin_bot

Update the spec config.yml to your own Mixin App(create in developers.mixin.one).

cd mixin_bot
mv spec/config.yml.example spec/config.yml

Run the test.

rake

References

License

This project rocks and uses MIT-LICENSE.