-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
122 lines (105 loc) · 2.75 KB
/
Copy pathapp.js
File metadata and controls
122 lines (105 loc) · 2.75 KB
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
const express = require('express');
const bodyParser = require("body-parser");
const fs = require("fs");
const axios = require("axios");
require("dotenv").config();
const redis = require("redis");
const client = redis.createClient(process.env.REDIS_URL, {
tls: {
rejectUnauthorized: false
}
})
const app = express();
const port = parseInt(process.env.PORT) || 3000;
const botId = process.env.BOT_ID;
app.use(bodyParser.json());
app.get('/', (req, res) => {
let scores = client.get("scores");
res.send(scores);
});
app.post('/', async (req, res) => {
let regex;
let text = req.body.text;
let data = req.body;
let commands = {
hello: /^\/say hello$/,
add: /^\/add( \d+)?$/,
scores: /^\/scores$/
}
if (!text || !data.sender_id) {
throw new Error("Text and user_id needed")
}
client.get("scores", (err, val) => {
if (err) {
res.send("Error connecting to database");
return;
}
else {
let scores
try {
scores = JSON.parse(val)
}
catch {
scores = {}
}
if (text.match(commands.hello)) {
res.send("Hello Cal");
}
else if (text.match(commands.add)) {
regex = text.match(commands.add);
let scoreIncr = parseInt(regex[1])
let user_id = String(data.sender_id);
let name = data.name;
if (scores[user_id] !== undefined) {
if (scoreIncr) {
scores[user_id]["score"] += scoreIncr;
}
else {
scores[user_id]["score"] ++;
}
}
else {
scores[user_id] = {
name: name,
score: isNaN(scoreIncr) ? 1 : scoreIncr
}
}
client.set("scores", JSON.stringify(scores));
Promise.resolve(answer(name + "---" + scores[user_id]["score"]))
.then(res.send(name + "---" + scores[user_id]["score"]))
.catch(err => {throw err});
}
else if (text.match(commands.scores)) {
let response = ""
Object.keys(scores).forEach(key => {
response = response + scores[key]["name"] + "---" + scores[key]["score"] + "\n";
});
if (response === "") {
response = "No Scores Available";
}
Promise.resolve(answer(response))
.then(res.send(response))
.catch(err => {throw err;});
}
else {
res.send("Could not understand");
}
}
});
})
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`)
})
module.exports = app;
answer = async function(message) {
return axios.post('https://api.groupme.com/v3/bots/post',
{
bot_id: botId,
text: message
}
)
.then(() => true)
.catch(err => {
throw err;
})
}