-
Notifications
You must be signed in to change notification settings - Fork 9
/
app2.js
140 lines (130 loc) · 6.05 KB
/
app2.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
const { SocketClient, isMessageType } = require('mixin-node-client');
const { HttpClient } = require('mixin-node-client');
const config = require('./config2');
const client = new SocketClient(config);
const ValidActions = ["ACKNOWLEDGE_MESSAGE_RECEIPT" ,"CREATE_MESSAGE", "LIST_PENDING_MESSAGES"];
const receiverID = "0b4f49dc-8fb4-4539-9a89-fb3afc613747";
const coinID = "6cfe566e-4aad-470b-8c9a-2fd35b49c68d";
console.log('Supported MessageSenders by SocketClient', client.getMessageSenders());
console.log(client.getMessageSenders());
// Listen and react to socket messages
client.on(
'message',
client.getMessageHandler(message => {
console.log('Message Received', message);
if (ValidActions.indexOf(message.action) > -1) {
if (message.action === 'ACKNOWLEDGE_MESSAGE_RECEIPT') {console.log("ignore receipt");return;}
if (isMessageType(message, 'text')) {
console.log('----------------------text-------------------------');
const text = message.data.data.toLowerCase();
if ( (message.data.category === "PLAIN_TEXT") && (message.action === "CREATE_MESSAGE") ) {
var parameter4IncomingMsg = {"message_id":message.data.message_id, "status":"READ"};
var RspMsg = {"id":client.getUUID(), "action":"ACKNOWLEDGE_MESSAGE_RECEIPT", "params":parameter4IncomingMsg};
client.sendRaw(RspMsg);
if (text === 'pay') {
var multiStr = ["usage: ",
"pay: show this message",
"pay1: send an APPBUTTONGROUP",
"pay2: send one button",
"pay3: send an APPCARD"].join("\n");
return client.sendText(multiStr,message);
}
//buttons
if (text === 'pay1') {
let payLinkEOS = "https://mixin.one/pay?recipient=" +
config.clientId + "&asset=" +
"6cfe566e-4aad-470b-8c9a-2fd35b49c68d" +
"&amount=0.01" + '&trace=' + client.getUUID() +
"&memo=";
let payLinkBTC = "https://mixin.one/pay?recipient=" +
config.clientId + "&asset=" +
"c6d0c728-2624-429b-8e0d-d9d19b6592fa" +
"&amount=0.01" + '&trace=' + client.getUUID() +
"&memo=";
return client.sendButtons([{
label: 'pay 0.01 EOS',
color: '#FF0000',
action: payLinkEOS,
},{
label: 'pay 0.001 BTC',
color: '#00ff00',
action: payLinkBTC,
}],message
);
}
//button
if (text === 'pay2') {
let payLink = "https://mixin.one/pay?recipient=" +
config.clientId + "&asset=" +
"6cfe566e-4aad-470b-8c9a-2fd35b49c68d" +
"&amount=0.01" + '&trace=' + client.getUUID() +
"&memo=";
return client.sendButton({
label: 'pay 0.01 EOS',
color: '#FF0000',
action: payLink,
},message);
}
if (text === 'pay3') {
let payLink = "https://mixin.one/pay?recipient=" +
config.clientId + "&asset=" +
"6cfe566e-4aad-470b-8c9a-2fd35b49c68d" +
"&amount=0.01" + '&trace=' + client.getUUID() +
"&memo=";
return client.sendApp({
icon_url: 'https://mixin.one/assets/98b586edb270556d1972112bd7985e9e.png',
title: 'pay 0.01 EOS',
description: '#FF0000',
action: payLink,
},message);
}
// todo: catch a error when balance insufficient
// (node:55535) UnhandledPromiseRejectionWarning: Error: Insufficient balance.
// at HttpClient.(anonymous function) [as createTransfer] (/Users/wenewzhang/Documents/sl/mixin_network-nodejs-bot2/node_modules/mixin-node-client/lib/http.js:99:23)
// at process.internalTickCallback (internal/process/next_tick.js:77:7)
if (text === 'refund') {
asyncRefundCall(coinID,'0.00001',receiverID);
}
return client.sendText( message.data.data, message);
}
}
if (message.data && message.data.category === "SYSTEM_ACCOUNT_SNAPSHOT" &&
message.action === 'CREATE_MESSAGE') {
console.log("-----------the bot got money!---------------");
var jsData = JSON.parse(Buffer.from(message.data.data, 'base64').toString('utf-8'));
console.log(jsData);
//let the server know that i have read this message
var parameter4IncomingMsg = {"message_id":message.data.message_id, "status":"READ"};
var RspMsg = {"id":client.getUUID(), "action":"ACKNOWLEDGE_MESSAGE_RECEIPT", "params":parameter4IncomingMsg};
client.sendRaw(RspMsg);
if (jsData.amount > 0) {
//refund immediately
asyncRefundCall(jsData.asset_id,jsData.amount,jsData.opponent_id);
} else console.log("refund success!");
console.log("-----------end of bot got money!---------------");
}
return Promise.resolve(message);
} else console.log("unknow action")
}));
client.on('error', err => console.error(err.message));
function refundInstant(_assetID,_amount,_opponent_id) {
return new Promise(resolve => {
var httpClient = new HttpClient(config);
const Obj = {
assetId: _assetID,
recipientId: _opponent_id,
traceId: httpClient.getUUID(),
amount: _amount,
memo: '',
};
console.log('resolve...');
console.log(Obj);
console.log("end of Obj");
httpClient.transferFromBot(Obj);
})
}
async function asyncRefundCall(_assetID,_amount,_opponent_id) {
console.log('calling asyncCall');
var result = await refundInstant(_assetID,_amount,_opponent_id);
console.log(result);
}