|
1 |
| -## Receive and send token |
| 1 | +# Chapter 2: Receive and send token |
2 | 2 | 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,
|
3 | 3 | 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, |
5 | 5 | >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) |
7 | 8 | 
|
8 | 9 |
|
9 | 10 | ```bash
|
@@ -33,10 +34,55 @@ wenewzha:mixin_network-nodejs-bot wenewzhang$ ./node_modules/mixin-cli/bin/mixin
|
33 | 34 | ```
|
34 | 35 | | Key | Description | example |
|
35 | 36 | | --- | -------------------------------------------- | -------------------------------------------------
|
36 |
| -| client secret | | 78ef86a80be17601f404ad643e5c85ed4f7f5f9f7a1597 | |
| 37 | +| client secret | generate from mixin.one's dashboard | 78ef86a80be17601f404ad643e5c85ed4f7f5f9f7a1597 | |
37 | 38 | | PIN | PIN code | 123456 |
|
38 | 39 | | PIN token | verify/update PIN | don't need here |
|
39 | 40 | | aesKey | generate by PIN token | GlJxnvlfhz7nxIk1eNkEdngf+jDW8XGHxJiaQTuD9v8= |
|
40 |
| -| clientSecret | generate from mixin.one's dashboard | 9873769d7b4198da2ee397af3ecaa87a5054a03d0114cedf28797567defa6fd8 | |
41 | 41 |
|
| 42 | + |
| 43 | +- **aesKey** generate aesKey by mixin-cli tool |
| 44 | +- **client secret** don't forget generate client secret |
42 | 45 | 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 | + |
| 72 | +otherwise, you can transfer any tokens to boy through message panel, the bot receive the tokens and then send back immediately. |
| 73 | + |
| 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! |
0 commit comments