-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_telegram.js
149 lines (132 loc) · 3.66 KB
/
client_telegram.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
const https = require('https')
const datastore = require('./client_datastore.js')
const creds_telegram = require('./credential/cred-telegram.json')
var options = {
hostname: 'api.telegram.org',
port: 443,
path: '',
method: 'POST',
headers: {
'Content-Type': 'application/json'
}
}
function stateDecrypt(state){
switch (state) {
case 0:
return 'In Progress ✍️';
break;
case 1:
return 'Code Review 📜';
break;
case 2:
return 'QA Testing 🤔';
break;
case 3:
return 'Ready to Deploy 💡';
break;
case 4:
return 'DONE ✅';
break;
default:
return state
}
}
function addTask(task, link, message) {
datastore.add(task, link, message.from.id)
var postData = JSON.stringify({
chat_id: message.chat.id,
reply_to_message_id: message.message_id,
text: 'Yes Sir!!'
})
sendRes(postData, 'sendMessage')
}
function updateTask(idtask, data, mode){
datastore.datastore.get(datastore.datastore.key(['task', parseInt(idtask)]), (err, entity) => {
var target_state = entity.state
if (mode == 'update') {
target_state += 1
} else {
target_state -= 1
}
if (target_state < 0 || target_state > 4) {
target_state = entity.state
}
datastore.update(idtask, target_state)
var postData = JSON.stringify({
callback_query_id: data.id
})
var initializePromise = sendRes(postData, 'answerCallbackQuery')
initializePromise.then(function(result) {
postData = JSON.stringify({
chat_id: data.message.chat.id,
message_id: data.message.message_id
})
var promiseDelete = sendRes(postData, 'deleteMessage')
promiseDelete.then(function(result) {
var prLink = '<a href=\"' + entity.link + '\">' + entity.task + '</a>';
prLink = prLink + ' as ' + ' <b>' + stateDecrypt(target_state) + '</b>\r\n'
postData = JSON.stringify({
chat_id: data.message.chat.id,
parse_mode: "HTML",
text: '@'+ data.from.username + ' ' + mode + ' ' + prLink
})
sendRes(postData, 'sendMessage')
})
})
});
}
function listTask(data, mode){
console.log(creds_telegram);
var postData = {
chat_id: data.chat.id,
reply_to_message_id: data.message_id,
text: '',
parse_mode: "HTML",
reply_markup: {
inline_keyboard: []
}
}
var result = datastore.get()
result
.then(results => {
const tasks = results[0];
console.log(tasks[0][datastore.datastore.KEY].id);
var row = -1;
for (var i = 0; i < tasks.length; i++) {
var prLink = '<a href=\"' + tasks[i].link + '\">' + tasks[i].task + '</a>';
var development = (i+1) + '. ' + prLink + ' <b>' + stateDecrypt(tasks[i].state) + '</b>\r\n'
postData.text += development
if (i%4 == 0) {
row += 1
postData.reply_markup.inline_keyboard.push([])
}
postData.reply_markup.inline_keyboard[row].push({
text: i+1,
callback_data: mode + ' ' + tasks[i][datastore.datastore.KEY].id,
})
}
sendRes(JSON.stringify(postData), 'sendMessage')
})
.catch(err => {
console.error('ERROR:', err);
});
}
function sendRes(postData, method) {
return new Promise(function(resolve, reject) {
options.headers['Content-Length'] = Buffer.byteLength(postData);
options.path = '/bot'+creds_telegram+'/'+method
const request = https.request(options, (respond) => {
resolve(respond);
})
request.on('error', (e) => {
reject(e);
});
request.write(postData);
request.end();
});
}
module.exports = {
addTask: addTask,
updateTask: updateTask,
listTask: listTask
}