How to create a Mixin Bot.

Yasmine Moustatia
Mixin Network
Published in
6 min readNov 29, 2018

This article is the first of a series of tutorials.

Why To Choose Mixin?

From BTC to ETH to EOS, I believe many engineers have some understanding of the blockchain and have heard about the story. As far as technology itself is concerned, whether it’s BitCoin’s underlying technology model, or starting a smart contract from ETH, or EOS’s unique architecture — it may make it difficult for engineers who first come into contact to feel that there is something wrong with it.

In fact, each public chain (claims) has its own unique application scenario, and specific technology is proposed for this. For developers, these technologies involving complex cryptography have become the entry barrier. What Mixin does is to smooth out the differences in the “transactions” of all public chains and focus on the “transactions” themselves .

In the Mixin Network, you can easily add the “Digital Money” attribute to your project using the most familiar technology stacks and protocols, enabling all “transactions” related functions such as “retail, reward, currency trading”. You can also use the DGN network of Mixin Network to add the (broadly) “blockchain” attribute to your own project, and realize all the “blockchain” related to “copyright, deposit, traceability, information disclosure”. Features.

In summary, using the Mixin Network allows your ideas to be easily implemented on the blockchain without the need to pay attention to many cumbersome technical details .

Tips

The so-called Dapp, short for Decentralized Application[¹]. Dapp is generally considered to be written by smart contracts (that is, code that runs on the blockchain).

Mixin has a different view on this. The supporters of Mixin believe in the Unix philosophy [²], “Let the program do only one thing”, and Mixin only needs to do the “Transaction”, so Mixin does not support smart contracts. That’s why you can write applications using your familiar technology stack, not just a few programming languages.

1- Register you App in Mixin Network

Before you start, you need to register your app in the Mixin Network. At the Mixin Developer Center at https://developers.mixin.one/dashboard, submit the basic information such as icon, name, description, URL, etc. to complete the registration. This is very similar to the common open service.

After registration is complete, click on “Click here to generate…” to produce the necessary keys and other information, as shown in the following figure:

The Mixin Network treats the App as a user, so User Id is used to uniquely identify the app on the network. Each app comes with an asset account and transfers directly to this User Id will go to the account.

The other information below, such as Client Secret, PIN, SessionId, Pin Token, Private Key, etc., is used to operate the account, perform update information, transfer, check, etc., and we will use it later. Be sure to remember them and don’t leak them.

I pre-registered a Hello Bot, and Mixin Id 7000101423. Can be added as a friend experience in Mixin Messenger.

Hello world.

Next, let’s write Hello world.

Task Description: The user tells the robot in the Mixin Messager that the syncrobot replies ack.

For the sake of simplicity, I will use MixinNetwork’s Bot SDK: in my project https://github.com/MixinNetwork/bot-api-go-client and use the Golang language for programming. So, let me assume that you have already arranged your Golang development environment and know how to write programs using Golang .

2. Prepare the SDK and project directory

The development environment for this article is macOS, and the version of Golang used is go1.11.2.

Let’s install the Bot SDK first.

Next, we may also need to install additional dependencies module, use the go getcommand install.

Prepare a directory for storing projects and source code, I would name it hello-bot; in which the new config.yml, for storing configuration; Finally, hello-bot under the new main.go, main code in them.

3. Understand how the Mixin Messenger robot works

Before you get started, let’s understand how the Mixin Messenger robot works.

Although the asset system of Mixin Messenger is built on the Mixin Network, the user and messaging system are independent. When using Mixin Messenger to chat, the server provides a relay of messages.

From the server’s perspective, the robot is like a regular user who is involved in a chat. Therefore, when chatting with the robot, the message is also forwarded by the server to the machine where the robot is located, and processed and answered by the robot program.

Since the robot is like a regular user, it also has its own wallet (available on App Dashboard https://developers.mixin.one/dashboard). Therefore, if a transfer is involved, the robot can operate its own account through the Mixin Network, and perform its own operations such as transferring funds.

4. Configure the robot

In the last chapter, “Registering App”, we got the message Id and private and other robots, it needs to be written to the configuration file config.yml into:

Please fill in the information as before. In which the private key private_keyis a multi-line text, be sure to follow the syntax yaml multiple lines of text, the lines to align the capital of each line .

In the source code, I provided it config.example.yaml and can modify it for your configuration file.

5. Receive a message

The core part of a robot is the message loop. This message loop constantly checks the server’s message queue for new messages.

We in main.go the main function, use the NewBlazeClient method to create a robot clients. clientIs a module-level variable for other function calls:

```go l // Create a bot client client = bot.NewBlazeClient( config.GetConfig().ClientID, config.GetConfig().SessionID, config.GetConfig().PrivateKey )

Among them, msgViewis the key parameter, which contains all the information related to this message. In the SDK source code , its definition is this:

Contains all the information needed for a message.

Robot processing a message, they need to msgView be disassembled, and then respond.

First, we determine only handle text messages, so the first to msgView.Category be judged. Then UniqueConversationId determine whether the message from the conversation partner is located.

Then msgView.Data be Base64 decoding, wherein the message content is acquired, to a variable inst in.

6. Reply to the message

Then we will process the message. In OnMessage the, for inst the content to judge:

If the message sent by the user is sync, then let the robot reply ask; if not sync, reply to the default text.

In reply to the realization Respond among function, it is very simple:

Just call the robot client client.SendPlainText return a word to each other, by the reply of the contents of variables msg specified.

The effect is as follows:

In this way, a robot that can talk to you is done. In OnMessage add more logic, you can make it with additional features, not just a word in reply.

Summary

The contents of this chapter are here for the time being. Hello Bot’s Mixin Id is 7000101423 a buddy experience that can be added to Mixin Messenger.

Hello Bot source code can be https://github.com/fox-one/hello-bot

--

--