-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.js
106 lines (93 loc) · 2.44 KB
/
bot.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
const Bot = require('./lib/Bot')
const SOFA = require('sofa-js')
const Fiat = require('./lib/Fiat')
let bot = new Bot()
// ROUTING
bot.onEvent = function(session, message) {
switch (message.type) {
case 'Init':
welcome(session)
break
case 'Message':
onMessage(session, message)
break
case 'Command':
onCommand(session, message)
break
case 'Payment':
onPayment(session, message)
break
case 'PaymentRequest':
welcome(session)
break
}
}
function onMessage(session, message) {
welcome(session)
}
function onCommand(session, command) {
switch (command.content.value) {
case 'ping':
pong(session)
break
case 'count':
count(session)
break
case 'donate':
donate(session)
break
}
}
function onPayment(session, message) {
if (message.fromAddress == session.config.paymentAddress) {
// handle payments sent by the bot
if (message.status == 'confirmed') {
// perform special action once the payment has been confirmed
// on the network
} else if (message.status == 'error') {
// oops, something went wrong with a payment we tried to send!
}
} else {
// handle payments sent to the bot
if (message.status == 'unconfirmed') {
// payment has been sent to the ethereum network, but is not yet confirmed
sendMessage(session, `Thanks for the payment! 🙏`);
} else if (message.status == 'confirmed') {
// handle when the payment is actually confirmed!
} else if (message.status == 'error') {
sendMessage(session, `There was an error with your payment!🚫`);
}
}
}
// STATES
function welcome(session) {
sendMessage(session, `Hello Token!`)
}
function pong(session) {
sendMessage(session, `Pong`)
}
// example of how to store state on each user
function count(session) {
let count = (session.get('count') || 0) + 1
session.set('count', count)
sendMessage(session, `${count}`)
}
function donate(session) {
// request $1 USD at current exchange rates
Fiat.fetch().then((toEth) => {
session.requestEth(toEth.USD(1))
})
}
// HELPERS
function sendMessage(session, message) {
let controls = [
{type: 'button', label: 'Ping', value: 'ping'},
{type: 'button', label: 'Count', value: 'count'},
{type: 'button', label: 'Donate', value: 'donate'}
]
session.reply(SOFA.Message({
body: message,
controls: controls,
showKeyboard: false,
}))
}