-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
78 lines (64 loc) · 2.59 KB
/
index.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
const express = require('express');
const { Telegraf } = require('telegraf');
var moment = require('moment');
const { google } = require('googleapis');
var _ = require('lodash');
require('dotenv').config();
const fs = require('fs');
const bot = new Telegraf(process.env.BOT_TOKEN);
const app = express();
// Read the contents of the JSON file
fs.readFile('google-api-credentials.json', 'utf8', (err, data) => {
if (err) {
console.error(err);
return;
}
// Parse the JSON data
const jsonData = JSON.parse(data);
// Log the JSON data to the console
console.log(jsonData);
});
const auth = new google.auth.GoogleAuth({
keyFile: 'google-api-credentials.json',
scopes: 'https://www.googleapis.com/auth/spreadsheets'
});
async function write(data) {
try {
//Create client instance
const client = await auth.getClient();
//Instance of google sheets api
const googleSheets = google.sheets({ version: 'v4', auth: client });
googleSheets.spreadsheets.values.append({
spreadsheetId: '1Ku5VfmmmsTGDzEUoPqUQ-Hdh0bD-mmSfF4u6O6sFj8I',
range: 'Gastos-Commit!A:J',
valueInputOption: 'RAW',
insertDataOption: 'INSERT_ROWS',
resource: data,
auth,
});
} catch (error) {
console.log(error);
}
}
bot.command('gasto', (ctx) => {
const regtime = moment().format('DD-MM-YYYY');
const str = ctx.message.text;
const spentReg = str.match(/(?:^\/\w+)(\s+)(?<worker>\w+)(\s+)(?<money>-?\d+)(\s+)+(?<notes>.+)/mu).groups;
const data = { values: [[regtime, '', spentReg.money, spentReg.worker, spentReg.notes, 'bot']] }
console.log("registro", spentReg, spentReg[1], spentReg.money);
console.log(data);
write(data);
ctx.reply(`persona: ${spentReg.worker}\n monto: ${spentReg.money}\n notas: ${spentReg.notes}\n fecha: ${regtime}`);
});
// Test telegram service with out test google services
bot.command('help', (ctx) => {
console.log("help command");
ctx.reply('Hola! soy el bot de gestion contable de commit_36 \n\n Consultas que puedes realizar: \n\n 1) Consultar sueldo antes de aportes \n\t/sueldo mes trabajador \n 2)Registrar gasto \n\t /gasto trabajador monto tipo de gasto')
});
const port = process.env.PORT || 1000;
app.use(bot.webhookCallback('/telegraf'));
bot.telegram.setWebhook('https://telegram-bot-g1vd.onrender.com/telegraf');
app.listen(port, () => console.log("Webhook bot listening on port", port));
// Enable graceful stop
process.once('SIGINT', () => bot.stop('SIGINT'))
process.once('SIGTERM', () => bot.stop('SIGTERM'))