-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.js
139 lines (122 loc) · 3.66 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
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
const fs = require('fs')
const { google } = require('googleapis')
const TelegramBot = require('node-telegram-bot-api')
const pointsBot = require('./pointsbot.js')
const markdown = require('markdown').markdown
let privateRooms = {}
// If modifying these scopes, delete credentials.json.
const SCOPES = ['https://www.googleapis.com/auth/spreadsheets']
// Load client secrets from a local file.
fs.readFile('client_secret.json', (err, content) => {
if (err) return console.log('Error loading client secret file:', err)
// Authorize a client with credentials, then call the Google Sheets API.
authorize(JSON.parse(content), authenticated)
})
// Make sure we have privateRooms.json
fs.writeFile('./privateRooms.json', '{}', { flag: 'wx' }, function(err) {})
fs.readFile('./privateRooms.json', 'utf8', function(err, data) {
if (!err) {
privateRooms = JSON.parse(data)
}
})
/**
* Create an OAuth2 client with the given credentials, and then execute the
* given callback function.
* @param {Object} credentials The authorization client credentials.
* @param {function} callback The callback to call with the authorized client.
*/
function authorize(credentials, callback) {
let jwtClient = new google.auth.JWT(
credentials.client_email,
null,
credentials.private_key,
SCOPES
)
//authenticate request
jwtClient.authorize(function(err, tokens) {
if (err) {
console.log(err)
return
} else {
console.log('Successfully connected!')
callback(jwtClient)
}
})
}
function authenticated(auth) {
fs.readFile('bot_credentials.json', (err, content) => {
if (err) return console.log('Error loading bot credentials', err)
content = JSON.parse(content)
const bot = new TelegramBot(content.token, { polling: true })
console.log('Logged in')
bot.on('polling_error', err => console.log(err))
bot.on('new_chat_members', msg => {
msg.new_chat_members.forEach(user => {
checkUser({ from: { id: user.id, username: user.username } })
})
savePrivateRooms()
})
bot.on('message', msg => {
checkUser(msg)
pointsBot.handlePointGiving(
auth,
msg,
privateRooms,
bot,
sendInternalMessage
)
savePrivateRooms()
})
})
}
// Make sure that every username-id pair is noted down
function checkUser(msg) {
if (
!Object.values(privateRooms).some(
u => u.room === msg.from.id && u.username === msg.from.username
)
) {
const existing = privateRooms[msg.from.username.toLowerCase()]
privateRooms[msg.from.username.toLowerCase()] = {
room: msg.from.id,
username: msg.from.username.toLowerCase(),
started: existing ? existing.started : false,
pendingNotifications: existing ? existing.pendingNotifications : [],
}
}
}
function sendInternalMessage(msg, user, client, callback) {
sendMessage(msg, user, client, privateRooms[user.toLowerCase()].room)
if (callback) {
callback()
}
}
function sendMessage(msg, user, client, room) {
if (msg.length > 0) {
msg = msg.replace(/^ +| +$/gm, '')
let html = markdown.toHTML(msg)
msg = msg.replace('%USER%', user)
html = html
.replace('%USER%', user)
.replace(/<p>/g, '\n')
.replace(/<\/p>/g, '')
client.sendMessage(room, html, { parse_mode: 'HTML' })
}
}
function savePrivateRooms() {
fs.writeFile(
'./privateRooms.json',
JSON.stringify(privateRooms, null, 2),
err => {
if (err) console.error(err)
}
)
}
// Zeit NOW workaround
const http = require('http')
http
.createServer((req, res) => {
res.setHeader('Content-Type', 'text/plain; charset=utf-8')
res.end('Hello there!')
})
.listen()