-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathset_function.js
38 lines (30 loc) · 1.38 KB
/
set_function.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
const Ain = require('@ainblockchain/ain-js').default;
const ain = new Ain('https://testnet-api.ainetwork.ai', 'wss://testnet-event.ainetwork.ai', 0);
// if you want to use mainnet, uncomment the following line:
// const ain = new Ain('https://mainnet-api.ainetwork.ai', 'wss://mainnet-event.ainetwork.ai', 1);
async function main() {
// import the account using private key
const address = ain.wallet.addAndSetDefaultAccount('YOUR_PRIVATE_KEY');
const appName = 'YOUR_APP_NAME'; // use your app name
const appPath = `/apps/${appName}`;
const functionPath = `${appPath}/messages/$user_addr/$timestamp/user`; // wild cards!
// set a function to be triggered when writing values at the function path
const res = await ain.db.ref(functionPath).setFunction({
value: {
'.function': {
'my-bot-trigger': {
function_type: 'REST',
function_url: 'http://testnet-echo-bot.ainetwork.ai/trigger', // function url for testnet
//function_url: 'http://mainnet-echo-bot.ainetwork.ai/trigger', // function url for mainnet
function_id: 'my-bot-trigger', // use your own function id
},
},
},
nonce: -1,
});
console.log('tx_hash:', res.tx_hash);
console.log('code:', res.result.code);
// 0: success, if not 0, check the error code:
// https://github.com/ainblockchain/ain-blockchain/blob/master/common/result-code.js
}
main();