-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
162 lines (129 loc) · 3.75 KB
/
app.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
const { App, AwsLambdaReceiver } = require('@slack/bolt');
const AWS = require('aws-sdk');
AWS.config.update({region:'us-west-2'}); // crash on local if this isn't set
const ddb = new AWS.DynamoDB.DocumentClient();
// Initialize your custom receiver
const awsLambdaReceiver = new AwsLambdaReceiver({
signingSecret: process.env.SLACK_SIGNING_SECRET,
});
// Initializes your app with your bot token and app token
const app = new App({
token: process.env.SLACK_BOT_TOKEN,
receiver: awsLambdaReceiver,
processBeforeResponse: true,
});
app.command('/prices', async ({ command, ack, respond }) => {
await ack();
await respond(`Still working on this feature. Contact <@U5LSGB3E2> for details.`);
});
app.action('subscribe', async ({ body, ack, say }) => {
await ack();
const threshold = body.actions[0].value;
await say(`Thanks <@${body.user.id}>, we'll notify you when gas hits ${threshold}`);
// Write this subscription to the DB
await writeSubscription(body.team.id, body.user.id, body.channel.id, threshold)
});
app.command('/start', async ({ command, ack, respond }) => {
await ack();
const frequency = command.text;
const enabled = true;
const channelid = command.channel_id
const teamid = command.team_id
if (frequency < 1) {
await respond('frequency must be > 1')
return
}
await respond(`Starting feed every ${frequency} minutes`);
// TODO: Write to DB (1) number of minutes (2) the teamid (3) channelid (4) status enabled/disabled
await writeGasbotTeams(
teamid,
channelid,
enabled,
frequency,
)
});
app.command('/alert', async ({ command, ack, respond }) => {
await ack();
await respond({
text: `<@${command.user_id}>, you'll be notified when gas is below ${command.text}.`,
});
await app.client.chat.postMessage({
channel: command.channel_id,
blocks: [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": `<@${command.user_id}> set an alert for when gas is below ${command.text}`
},
},
{
"type": "actions",
"block_id": "actionblock789",
"elements": [
{
"type": "button",
"text": {
"type": "plain_text",
"text": "Subscribe to this alert"
},
"style": "primary",
"value": command.text,
"action_id": "subscribe",
}
]
}
]
})
});
app.message('goodbye', async ({ message, say }) => {
// say() sends a message to the channel where the event was triggered
await say(`See ya later, <@${message.user}> :wave:`);
});
const writeGasbotTeams = async (teamid, channelid, enabled, frequency) => {
const TableName = 'gasbotteams';
const params = {
TableName,
Item: {
teamid,
channelid,
enabled,
frequency
}
};
return ddb.put(params).promise();
}
const writeSubscription = async (teamid, userid, channelid, threshold) => {
const TableName = 'gasbot'
const date = new Date();
const epoch = date.getTime();
// converting back to date-time
const timestamp = new Date(epoch).getTime()
// Get original list if any
const originalRecord = await ddb.get({
TableName,
Key: {
teamid,
},
}).promise();
const isRecordExistant = originalRecord && originalRecord.Item && originalRecord.Item.subscribers;
const databaseSubscribers = isRecordExistant ? originalRecord.Item.subscribers.values : [];
databaseSubscribers.push(userid)
const subscribers = Array.from((new Set(databaseSubscribers)).values())
const params = {
TableName,
Item: {
subscribers: ddb.createSet(subscribers),
teamid,
threshold,
timestamp,
channelid,
}
};
return ddb.put(params).promise();
}
// Handle the Lambda function event
module.exports.handler = async (event, context, callback) => {
const handler = await awsLambdaReceiver.start(3000);
return handler(event, context, callback);
}