Skip to content

Commit de6420f

Browse files
committed
transfer token and send it back immediately
1 parent 28db91c commit de6420f

File tree

4 files changed

+57
-8
lines changed

4 files changed

+57
-8
lines changed

README.md

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# Step by step: How to use nodejs to create a bot in Mixin Messenger
2-
In this charpter, you can create a bot in Mixin Messenger to receive user message after you following the guide. In next chapter, your bot can receive token from user and pay token to user .
2+
In this chapter, you can create a bot in Mixin Messenger to receive user message after you following the guide. In next chapter, your bot can receive token from user and pay token to user .
33

44

5-
[Mixin Network](https://mixin.one) is a free and lightning fast peer-to-peer transactional network for digital assets.
5+
[Mixin Network](https://mixin.one) is a free and lightning fast peer-to-peer transactional network for digital assets.
66

77
These articles will show you how to write a bot in nodejs. The bot can receive and response to user's message. User can pay token to bot and bot can transfer token to user.
88

@@ -22,7 +22,7 @@ apt install node yarn
2222
```
2323

2424

25-
### Create you first bot
25+
### Create you first bot
2626
Before you write any code, you need to create an app by following [tutorial](https://mixin-network.gitbook.io/mixin-network/mixin-messenger-app/create-bot-account).
2727

2828
write down three required infomations: user id, session id, private key, mixin-node sign the token with them.
@@ -149,3 +149,6 @@ install [Mixin Messenger](https://mixin.one/),add the bot as your friend,(for ex
149149
enjoy!
150150
151151
![mixin_messenger](https://github.com/wenewzhang/mixin_network-nodejs-bot2/blob/master/mixin_messenger-sayhi.png)
152+
153+
154+
##[Chapter 2: Receive and send token](https://github.com/wenewzhang/mixin_network-nodejs-bot2/blob/master/README2.md)

README2.md

+51-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
## Receive and send token
1+
# Chapter 2: Receive and send token
22
In this chapter, the bot can receive token from user and then pay it back immediately, you must know the miss options in the config.js file,
33
here show you how to generate a client secret, PIN, aesKey etc.
4-
>if you had readed the python, javescript or php code, you should find this config.js doesn't have PIN token, yes, nodejs use PIN token generate aesKey,
4+
>if you have read the python, javescript or php code, you should find this config.js doesn't have PIN token, yes, nodejs use PIN token generate aesKey,
55
>i show you how to do it
6-
### First, copy the PIN,session id,private key from [Mixin.one Dashboard](https://developers.mixin.one/dashboard)
6+
## First, generate a completely config.js
7+
copy the PIN,session id,private key etc. from [Mixin.one Dashboard](https://developers.mixin.one/dashboard)
78
![copy config info from dashboard](https://github.com/wenewzhang/mixin_network-nodejs-bot2/blob/master/copy-to-clipboard.png)
89

910
```bash
@@ -33,10 +34,55 @@ wenewzha:mixin_network-nodejs-bot wenewzhang$ ./node_modules/mixin-cli/bin/mixin
3334
```
3435
| Key | Description | example |
3536
| --- | -------------------------------------------- | -------------------------------------------------
36-
| client secret | | 78ef86a80be17601f404ad643e5c85ed4f7f5f9f7a1597 |
37+
| client secret | generate from mixin.one's dashboard | 78ef86a80be17601f404ad643e5c85ed4f7f5f9f7a1597 |
3738
| PIN | PIN code | 123456 |
3839
| PIN token | verify/update PIN | don't need here |
3940
| aesKey | generate by PIN token | GlJxnvlfhz7nxIk1eNkEdngf+jDW8XGHxJiaQTuD9v8= |
40-
| clientSecret | generate from mixin.one's dashboard | 9873769d7b4198da2ee397af3ecaa87a5054a03d0114cedf28797567defa6fd8 |
4141

42+
43+
- **aesKey** generate aesKey by mixin-cli tool
44+
- **client secret** don't forget generate client secret
4245
completely config.js can find [here](https://github.com/wenewzhang/mixin_network-nodejs-bot2/blob/master/config2.js)
46+
47+
## Source code brief explanation
48+
> app2.js define acceptable actions
49+
```javascript
50+
const ValidActions = ["ACKNOWLEDGE_MESSAGE_RECEIPT" ,"CREATE_MESSAGE", "LIST_PENDING_MESSAGES"];
51+
```
52+
53+
create a payment link to user when user ask 'pay'
54+
```javascript
55+
if (text === 'pay') {
56+
let payLink = "https://mixin.one/pay?recipient=" +
57+
config.clientId + "&asset=" +
58+
"6cfe566e-4aad-470b-8c9a-2fd35b49c68d" +
59+
"&amount=0.01" + '&trace=' + client.getUUID() +
60+
"&memo=";
61+
return client.sendButton({
62+
label: 'pay 0.01 EOS',
63+
color: '#FF0000',
64+
action: payLink,
65+
},
66+
message
67+
);
68+
}
69+
```
70+
you can pay 0.01 EOS to bot through pay command,
71+
![pay-link](https://github.com/wenewzhang/mixin_network-nodejs-bot2/blob/master/pay-link.png)
72+
otherwise, you can transfer any tokens to boy through message panel, the bot receive the tokens and then send back immediately.
73+
![transfer and tokens](https://github.com/wenewzhang/mixin_network-nodejs-bot2/blob/master/transfer-any-tokens.jpeg)
74+
75+
```javascript
76+
if (message.data && message.data.category === "SYSTEM_ACCOUNT_SNAPSHOT") {
77+
var jsData = JSON.parse(Buffer.from(message.data.data, 'base64').toString('utf-8'));
78+
//let the server know that i have readed this message
79+
var parameter4IncomingMsg = {"message_id":message.data.message_id, "status":"READ"};
80+
var RspMsg = {"id":client.getUUID(), "action":"ACKNOWLEDGE_MESSAGE_RECEIPT", "params":parameter4IncomingMsg};
81+
client.sendRaw(RspMsg);
82+
if (jsData.amount > 0) {
83+
//refund immediately
84+
asyncRefundCall(jsData.asset_id,jsData.amount,jsData.opponent_id);
85+
} else console.log("refund success!");
86+
}
87+
```
88+
if jsData.amount is negative, that's mean send the token back success!

pay-link.png

238 KB
Loading

transfer-any-tokens.jpeg

157 KB
Loading

0 commit comments

Comments
 (0)