-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtelegram.js
More file actions
116 lines (85 loc) · 2.2 KB
/
telegram.js
File metadata and controls
116 lines (85 loc) · 2.2 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
"use strict";
var coffeescript = require('coffee-script/register')
var ChatHelper = require("./chathelper").ChatHelper;
var jsonfile = require('jsonfile')
var path = require('path')
var configPath= path.join(__dirname,"config.json")
var config = jsonfile.readFileSync(configPath,{"throws":false});
if(!config || ! config.token){
console.log("please put config.json in ",configPath,", contents like {token:'your telegram bot api token'}");
return;
}
var telegram = require('telegram-bot-api');
var api = new telegram({
token: config.token,
updates: {
enabled: true,
get_interval: 1000
},
http_proxy:config.http_proxy
});
api.getMe()
.then(function(data)
{
console.log(data);
})
.catch(function(err)
{
console.log(err);
});
function telegram_2_zeronet(message,chatController){
var from ="";
if(message.chat && message.chat.title){
from=message.chat.title+":";
}
if(message.from){
from = from+ message.from.first_name;
if(message.from.last_name){
from=from+" "+message.from.last_name;
}
from = from+":";
}
chatController.addMessage(
{
body:from+message.text,
date_added:message.date*1000
});
}
function publishZeronet(chatController){
if(chatController.changed){
console.log("publish");
chatController.recycle();
chatController.save(false);
}
}
function collectFunc(chatController){
setInterval(function(){
publishZeronet(chatController);
},60000);
console.log("setting up message callback");
api.on('message', function(message)
{
var chat_id = message.chat.id;
// It'd be good to check received message type here
// And react accordingly
// We consider that only text messages can be received here
if(!message.text){
console.log("not supported type");
return;
}
telegram_2_zeronet(message,chatController);
});
}
new ChatHelper(
{
addr:"1iMFEmmY4xyWTGi3Doot98nxeB2sqeEtX",
host:"127.0.0.1",
port:43110,
proto:{
ws:"ws",
http:"http"
}
},
["zeroid.bit","web","telegrambot","1My4AHpEjVQB1Jvetpiuha7o2QtQgWHL5L"]
,true,
collectFunc);