-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.js
More file actions
210 lines (162 loc) · 5.13 KB
/
Copy pathbuild.js
File metadata and controls
210 lines (162 loc) · 5.13 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
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
// SPDX-License-Identifier: MIT
"use strict";
const axios = require("axios");
const pageBreakMarkup = "[pagebreak]";
const buttonMarkupStart = "[button|";
const buttonMarkupSeparator = "|";
const buttonMarkupEnd = "]";
const separatorMarkup = "[row]";
const urlMarkup = "http";
const linkMarkup = "<a href";
function isMediaMessage(message) {
const urlCount = message.split(urlMarkup).length - 1;
const linkCount = message.split(linkMarkup).length - 1;
const mediaUrlCount = urlCount - linkCount;
if (urlCount === 0) {
// No URLs
return false;
} else if (mediaUrlCount > 0) {
// At least one more URL than links, so this is a media message
return true;
}
// The same number of URLs and links, so they are all links
return false;
}
function breakApartMedia(message) {
const containsUrl = message.includes(urlMarkup);
if (containsUrl !== true) {
const splitMessage = [message];
return splitMessage;
}
const urlCount = message.split(urlMarkup).length - 1;
const linkCount = message.split(linkMarkup).length - 1;
if (urlCount === linkCount) {
const splitMessage = [message];
return splitMessage;
}
const splitMessage = [];
// Break off the front part of the message
const mediaStart = message.indexOf(urlMarkup);
const frontString = message.slice(0, mediaStart).trimEnd();
if (frontString.length > 0) {
splitMessage.push(frontString);
}
// Break off the media
const restOfString = message.slice(mediaStart);
const mediaEnd = restOfString.indexOf(" ");
if (mediaEnd === -1) {
splitMessage.push(restOfString);
} else {
const mediaMessage = restOfString.slice(0, mediaEnd);
splitMessage.push(mediaMessage);
// break off the end part of the message
const endString = restOfString.slice(mediaEnd).trim();
if (endString.length > 0) {
splitMessage.push(endString);
}
}
return splitMessage;
}
function mediaSplitter(responses) {
const replyMessages = [];
for (let i = 0; i < responses.length; i++) {
const message = responses[i];
const splitMessage = breakApartMedia(message);
for (let i = 0; i < splitMessage.length; i++) {
const reply = splitMessage[i];
replyMessages.push(reply);
}
}
return replyMessages;
}
function splitReply(reply) {
// Split based on page breaks
const pageSplits = reply.split(pageBreakMarkup);
return pageSplits;
}
function parseButton(rawButtonText) {
const buttonSeparator = rawButtonText.indexOf(buttonMarkupSeparator);
const buttonValues = rawButtonText.slice(buttonSeparator + 1);
const separator = buttonValues.indexOf(buttonMarkupSeparator);
const buttonEnd = buttonValues.indexOf(buttonMarkupEnd);
const buttonLabel = buttonValues.slice(0, separator);
const buttonAction = buttonValues.slice(separator + 1, buttonEnd);
return { label: buttonLabel, action: buttonAction };
}
function splitButtons(response) {
const buttons = [];
let reply = "";
let content = response;
while (content.length) {
const separatorStart = content.indexOf(separatorMarkup);
const buttonStart = content.indexOf(buttonMarkupStart);
const bothFound =
separatorStart !== -1 &&
buttonStart !== -1 &&
separatorStart < buttonStart;
const onlySeparatorFound = separatorStart !== -1 && buttonStart === -1;
const separatorFound = bothFound || onlySeparatorFound;
const buttonFound = buttonStart !== -1;
if (separatorFound) {
const separatorEnd = separatorStart + separatorMarkup.length;
content = content.slice(separatorEnd);
const row = { label: "row", action: "separator" };
buttons.push(row);
} else if (buttonFound) {
const frontOfString = content.slice(0, buttonStart);
reply = reply + frontOfString;
const restOfString = content.slice(buttonStart);
const buttonEnd = restOfString.indexOf(buttonMarkupEnd);
content = restOfString.slice(buttonEnd + 1);
const rawButtonText = restOfString.slice(0, buttonEnd + 1);
const button = parseButton(rawButtonText);
buttons.push(button);
} else {
reply = reply + content;
content = "";
}
}
return { reply, buttons };
}
function buildKeyboard(buttons) {
const buttonGrid = [];
let buttonRow = [];
for (let i = 0; i < buttons.length; i++) {
const button = buttons[i];
if (button.label === "row" && button.action === "separator") {
buttonGrid.push(buttonRow);
buttonRow = [];
} else {
buttonRow.push({ text: button.label, callback_data: button.action });
}
}
if (buttonRow.length > 0) {
buttonGrid.push(buttonRow);
}
const keyboard = {
inline_keyboard: buttonGrid,
};
return keyboard;
}
function buildConfig() {
const config = {
headers: {
"Content-Type": "application/json",
},
};
return config;
}
async function sendPayload(body, endpoint, telegramToken) {
const baseUrl = "https://api.telegram.org/bot";
const fullUrl = `${baseUrl}${telegramToken}/${endpoint}`;
const config = buildConfig();
return axios.post(fullUrl, body, config);
}
module.exports = {
isMediaMessage,
splitReply,
splitButtons,
mediaSplitter,
buildKeyboard,
sendPayload,
};