-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhandler.js
185 lines (148 loc) · 4.36 KB
/
handler.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
'use strict';
const request = require('request-promise');
const aws = require('aws-sdk')
const {google} = require('googleapis');
const parseMessage = (event) => {
var body = JSON.parse(event.body)
return Object.assign(event, {body: body})
}
const getGoogleToken = (event) => {
const credentials = JSON.parse(process.env.GOOGLE_CREDENTIALS);
const {client_secret, client_id, redirect_uris} = credentials.installed;
const oAuth2Client = new google.auth.OAuth2(
client_id, client_secret, redirect_uris[0]);
oAuth2Client.setCredentials(JSON.parse(process.env.GOOGLE_TOKEN));
return Object.assign(event, {'auth': oAuth2Client});
}
const buildResponse = (event) => {
var text = event.body.message.text
// capture 2 groups: (amount) (description)
var regex = /(\d+(?:\.\d+)?)\s((?:\w+)(?:\s\w+)*)/
var result = text.match(regex)
var response = ''
var expense = ''
if (result) {
expense = {
amount: result[1],
categories: result[2]
}
}
if (text == '/start') {
response = 'Welcome'
} else if (text == '/help') {
response = 'Send messages in format> amount categories'
} else if (expense) {
response = `Got it ${expense.amount} added (${expense.categories})`
} else {
response = `I didn't understand. Try /help.`
}
return Object.assign(event, {response: response, expense: expense})
};
const storeExpense = (event) => {
if (!event.expense) {
return event
}
const timestamp = new Date().getTime()
const sheets = google.sheets({version: 'v4', auth: event.auth})
return new Promise((resolve, reject) => {
sheets.spreadsheets.values.append({
spreadsheetId: process.env.GOOGLE_SHEET_ID,
range: getSheetName(event.body.message.date)+'!A:C',
valueInputOption: 'USER_ENTERED',
insertDataOption: 'INSERT_ROWS',
resource: {
majorDimension: 'ROWS',
values: [
[formatDate(event.body.message.date), event.expense.amount, event.expense.categories]
]
}
}, (err, {data}) => {
if (err) reject(err)
resolve(Object.assign(event, {data: data}))
});
})
}
const addNewSheet = (event) => {
const sheets = google.sheets({version: 'v4', auth: event.auth})
return new Promise((resolve, reject) => {
sheets.spreadsheets.values.get({
spreadsheetId: process.env.GOOGLE_SHEET_ID,
range: getSheetName(event.body.message.date)+'!A:C'
}, (err, {data}) => {
if (err) {
sheets.spreadsheets.batchUpdate({
spreadsheetId: process.env.GOOGLE_SHEET_ID,
resource: {
requests: [
{
addSheet: {
properties: {
title: getMonthName(event.body.message.date)
}
}
}
]
}
}, (err2, {data2}) => {
if (err2) {
reject(err2)
} else {
resolve(event)
}
})
} else {
resolve(event)
}
})
})
}
const sendMessage = (event) => {
var chatId = event.body.message.chat.id
const url = `https://api.telegram.org/bot${process.env.TELEGRAM_TOKEN}/sendMessage`
var body = {
chat_id: chatId,
text: event.response
}
var options = {
method: 'POST',
uri: url,
body: body,
json: true
}
return request(options)
};
const successResponse = callback => callback(null, {
statusCode: 200
})
const errorResponse = (error, callback) => {
return callback(null, {
statusCode: 500
})
}
const getSheetId = (timestamp) => {
const date = new Date(timestamp*1000)
return date.getMonth()+date.getFullYear()
}
const getSheetName = (timestamp) => {
return "'" + getMonthName(timestamp) + "'"
}
const getMonthName = (timestamp) => {
const date = new Date(timestamp*1000)
const locale = "en-us"
return date.toLocaleString(locale, { month: "long" })
+ ' ' + date.toLocaleString(locale, {year: "2-digit"})
}
const formatDate = (timestamp) => {
const date = new Date(timestamp*1000)
return date.getDate()+'/'+date.getMonth()+'/'+date.getFullYear()
}
module.exports.hello = (event, context, callback) =>
Promise.resolve(event)
.then(parseMessage)
.then(buildResponse)
.then(getGoogleToken)
.then(addNewSheet)
.then(storeExpense)
.then(sendMessage)
.then(() => successResponse(callback))
.catch(error => errorResponse(error, callback))