-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
115 lines (95 loc) Β· 3.03 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
const https = require("https");
const mqtt = require("mqtt");
const querystring = require("querystring");
const config = require("./config.json");
class Telegram {
constructor(config) {
this.someoneWaiting = false;
this.latestUser = "";
this.lastMessageId = 0;
this.storedMessage = "";
this.config = config;
this.postToTelegram("π€ Hackscreen is starting, waiting for MQTT...");
this.MQTTclient = mqtt.connect(config.mqtt.server);
this.MQTTclient.on("connect", props => this.mqttHasConnected(props));
this.MQTTclient.on("message", (topic, message) =>
this.mqttReceivedMessage(topic, message)
);
}
mqttHasConnected() {
console.log(this.MQTTclient);
this.MQTTclient.subscribe("door/#");
this.postToTelegram("π MQTT connected");
}
mqttReceivedMessage(topic, message) {
switch (topic) {
case "door/outer/opened/username":
let justEntered = decodeURIComponent(message.toString());
if (!justEntered || justEntered == "") justEntered = "π»";
if (justEntered == "-" || justEntered == "anon") break;
const now = new Date();
const niceDate = now.toLocaleTimeString();
if (justEntered === this.latestUser) {
this.storedMessage = `${this.storedMessage} (${niceDate})`;
this.postToTelegram(this.storedMessage, this.lastMessageId, () => {});
} else {
this.latestUser = justEntered;
this.storedMessage = `π ${justEntered} (${niceDate})`;
this.postToTelegram(this.storedMessage, false, id => {
this.lastMessageId = id;
});
}
break;
case "door/outer/state":
this.someoneWaiting && this.postToTelegram(` ${message.toString()}`);
this.someoneWaiting = false;
break;
case "door/outer/doorbell":
this.someoneWaiting = true;
this.postToTelegram(`π doorbell!`);
break;
default:
break;
}
}
postToTelegram(message, message_id, callback) {
const postData = querystring.stringify({
chat_id: this.config.telegram.chat_id,
message_id,
text: message
});
const options = {
hostname: `api.telegram.org`,
port: 443,
path: `/bot${this.config.telegram.token}/${
message_id ? "editMessageText" : "sendMessage"
}`,
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
"Content-Length": postData.length
}
};
const req = https.request(options, res => {
console.log("statusCode:", res.statusCode);
console.log("headers:", res.headers);
let body = "";
res.on("data", d => {
body += d;
});
res.on("end", function() {
var parsed = JSON.parse(body);
if (parsed && parsed.ok) {
console.log(parsed);
callback && callback(parsed.result.message_id);
}
});
});
req.on("error", e => {
console.error(e);
});
req.write(postData);
req.end();
}
}
new Telegram(config);