-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
289 lines (215 loc) · 7.62 KB
/
app.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
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
const Bot = require('node-telegram-bot-api');
const dotenv = require('dotenv');
const mongoose = require('mongoose');
const camera = require('./camera');
const fritzbox = require('./fritzbox');
const vfb = require('./vfb');
const temp = require('./temperature');
const smartsocket = require('./smartsocket');
const youtube = require('./youtube');
// CONFIGS
dotenv.load({ path: '.env' });
mongoose.connect(process.env.MONGODB_URI || process.env.MONGOLAB_URI);
mongoose.connection.on('error', () => {
process.exit();
});
const token = process.env.TelegramAPI;
// Create bot
const dude = new Bot(token, { polling: true });
// News Counter
var newsCounter = 0;
// SMART SOCKET
dude.onText(/\/sockets/, (msg) => {
const fromId = msg.from.id;
const options = {
parse_mode: 'HTML',
disable_web_page_preview: true,
reply_markup: {
inline_keyboard: []
}
};
smartsocket.getSockets().then(sockets => {
for (const socket of sockets) {
var value ;
if (socket.mode != 0) {
// ON
options.reply_markup.inline_keyboard.push([
{
text : socket.name+" ✅",
callback_data : JSON.stringify(
{
text: socket._id,
type: "sockets"
}
)
}
])
}
else {
// OFF
options.reply_markup.inline_keyboard.push([
{
text : socket.name+" ❎",
callback_data : JSON.stringify(
{
text: socket._id,
type: "sockets"
}
)
}
])
}
}
dude.sendMessage(fromId, "Welche Steckdose soll eingeschalten werden?", options);
})
});
dude.on('callback_query', function (msg) {
var data = JSON.parse(msg.data);
if(data.type == "vfb"){
var newsID = data.nr+1;
vfb.loadFeed().then(data => {
dude.answerCallbackQuery(msg.id, 'Neue Newsinhalte wurden bereitgestellt!');
const title = data.feed.items[newsID].title;
const description = data.feed.items[newsID].description.replace(/<\/?[^>]+(>|$)/g,"").trim();
const link = data.feed.items[newsID].link;
dude.editMessageText("<b>"+title+"</b>\n"+description+"\n\n<a href='"+link+"'>Artikel lesen</a>",
{
chat_id: msg.from.id,
message_id: msg.message.message_id,
parse_mode: 'HTML',
disable_web_page_preview: true,
reply_markup: {
inline_keyboard: [
[
{
text: "Show more News",
callback_data: JSON.stringify(
{
text: "more",
type: "vfb",
nr: newsID
}
)
}
],
]
}
});
});
}
if(data.type == "sockets"){
const options = {
inline_keyboard: []
};
smartsocket.turnSocket(data.text).then(value => {
dude.answerCallbackQuery(msg.id, 'Ok, Steckdose wurde angsteuert!!!');
smartsocket.getSockets().then(sockets => {
for (const socket of sockets) {
var value ;
if (socket.mode != 0) {
// ON
options.inline_keyboard.push([
{
text : socket.name+" ✅",
callback_data : JSON.stringify(
{
text: socket._id,
type: "sockets"
}
)
}
])
}
else {
// OFF
options.inline_keyboard.push([
{
text : socket.name+" ❎",
callback_data : JSON.stringify(
{
text: socket._id,
type: "sockets"
}
)
}
])
}
}
dude.editMessageText("Steckdose wurde angesteuert!",
{
chat_id: msg.from.id,
message_id: msg.message.message_id,
reply_markup: options
});
});
});
}
});
// YOUTUBE
dude.on('message', function(msg) {
console.log("message", msg);
if (msg.entities[0].type == "url" && msg.entities[0].length == 42) {
youtube.downloadVideo(msg.text).then( value =>
{
dude.sendMessage(msg.from.id, `${value} erfolgreich heruntergeladen!`);
}, reason => {
dude.sendMessage(msg.from.id, `Song wurde bereits heruntergeladen!`);
})
}
});
// CAMERA
dude.onText(/\/snap/, (msg) => {
const fromId = msg.from.id;
camera.makePhoto().then(value => {
dude.sendPhoto(fromId, value);
});
});
// FRITZBOX
dude.onText(/\/home/, (msg) => {
const fromId = msg.from.id;
dude.sendMessage(fromId, "Folgende Geräte sind verbunden:");
fritzbox.getCurrentlyHomeDevices().then(devices => {
for (const device of devices) {
dude.sendMessage(fromId, device.name);
}
});
});
// TEMPERATURE
dude.onText(/\/temperature/, (msg) => {
const fromId = msg.from.id;
temp.getHomeTemperature().then(value => {
dude.sendMessage(fromId, "Die Temperatur beträgt "+value+"°C");
});
});
// VFB
dude.onText(/\/vfbnews/, (msg) => {
var chatId = msg.from.id;
newsCounter = 0;
dude.sendMessage(chatId, "Ok! Hier ein paar News vom VfB Stuttgart");
const options = {
parse_mode: 'HTML',
disable_web_page_preview: true,
reply_markup: {
inline_keyboard: [
[
{
text: "Show more News",
callback_data: JSON.stringify(
{
text: "more",
type: "vfb",
nr: 0
}
)
}
],
]
}
};
vfb.loadFeed().then(data => {
const title = data.feed.items[newsCounter].title;
const description = data.feed.items[newsCounter].description.replace(/<\/?[^>]+(>|$)/g,"").trim();
const link = data.feed.items[newsCounter].link;
dude.sendMessage(chatId, "<b>"+title+"</b>\n"+description+"\n\n<a href='"+link+"'>Artikel lesen</a>", options);
});
});