-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
89 lines (73 loc) · 2.24 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
// import telegram lib
const Telegraf = require('telegraf').Telegraf
require('dotenv').config()
// Am folosit .env pentru a pastra TOKEN-ul botului in siguranta (commit-uri)
// Instantierea botuluib de Telegram folosind TOKEN-ul (din aplicatie)
const bot = new Telegraf(process.env.BOT_TOKEN)
// Mesajele de reply (initializare stringuri)
// pentru bot-ul de Telegram
// Mesaj de hello
const welcomeMessage = 'Welcome!🐱🐶 I am an Automatic Feeder Robot. Send me a command so I can feed your pet.'
// Mesaj de help
const helpMessage = 'You can control me by sending these commands:' +
'\n\n' +
'/start - short intro\n' +
'/help - show all the commands available\n' +
'/feed - feed my Kara\n' +
'/credits - and the 1st prize goes to ...\n'
// Mesaj de credits
const creditsMessage = 'UPB-ACS project made by:\n\n' +
'👩💻 Ciuciu Anca-Maria\n' +
'🏫 Facultatea de Automatică și Calculatoare\n'
/* /start command */
bot.start((ctx) => ctx.reply(welcomeMessage))
/* /help command */
bot.help((ctx) => ctx.reply(helpMessage))
/* /credits command */
bot.command('credits', ctx => {
console.log(ctx.from)
bot.telegram.sendMessage(ctx.chat.id, creditsMessage, {
})
})
/* /feed command */
bot.command('feed', ctx => {
if (GLOBALSOCKET != undefined) {
sendReply('f');
ctx.reply("Ok, feeding cat now!");
}
else {
ctx.reply("Bot not connected!");
}
})
// Run the bot
bot.launch() // start
var net = require('net');
GLOBALSOCKET = undefined
function sendReply(data){
GLOBALSOCKET.write(data);
}
var isPressed = false;
var server = net.createServer(function(socket) {
socket.write('Echo server!!!\r\n');
socket.on('error', function(err) {
console.log("Error")
GLOBALSOCKET = undefined
})
socket.on('close', function(){
console.log("Socked closed")
GLOBALSOCKET = undefined
})
socket.on('data', function(data) {
var isPressed = false;
for (const item of data) {
if (item == 120) {
isPressed = true;
}
}
if (isPressed) {
bot.telegram.sendMessage(process.env.TELEGRAM_ID, "Kara was fed by herself.")
}
});
GLOBALSOCKET = socket;
});
server.listen(1337, '0.0.0.0');