-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.js
88 lines (75 loc) · 1.68 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
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)
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) {
sendMessage(session, `Thanks for the 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,
}))
}