-
Notifications
You must be signed in to change notification settings - Fork 4
/
telegram.js
196 lines (150 loc) · 6.68 KB
/
telegram.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
var proj_config = require('./config'),
rpc_server = "http://"+proj_config.rpc_server+":"+proj_config.rpc_port,
TeleBot = require('telebot'),
bot = new TeleBot(proj_config.api_key),
/*web3 = require('web3'),*/
Blockchain = require('./Blockchain'),
TheBankContract = Blockchain.TheBank,
http = require('http'),
fs = require('fs'),
randomstring = require("randomstring"),
request = require('request'),
qr_image_format = 'png',
min_bal_required = 0,
mongoose = require('mongoose'),
SGB = require('./models/sgb'),
Transaction = require('./models/transactions');
// The SGBs lying in a circle with user's location and this radius (in KM) are queried for in the database
const km_radius = 3;
const map_marker_colors = ['blue','green','red','orange'];
bot.use(require('./node_modules/telebot/modules/ask.js'));
// Buttons
bot.on(['/hello','/back'], msg => {
let markup = bot.keyboard([
['/throw', '/collect','/back', '/hide']
], { resize: true });
return bot.sendMessage(msg.from.id, 'Smart Waste Management Menu.', { markup });
});
// Hide keyboard
bot.on('/hide', msg => {
return bot.sendMessage(
msg.from.id, 'Hide keyboard example. Type /back to show.', { markup: 'hide' }
);
});
bot.on('/throw', msg => {
const id = msg.from.id;
// Ask user name
return bot.sendMessage(id, 'Please enter your Ethereum account: ', { ask: 'throw' });
});
bot.on('ask.throw', msg => {
let fromId = msg.from.id;
chat_id = fromId;
let firstName = msg.from.first_name;
let reply = msg.message_id;
//let text = msg.text.split(" ");
//let account = text[1];
let account = msg.text;
//let waste_type = text[2].toUpperCase();
let waste_type = "ORGANIC";
let balance = TheBankContract.balanceOf.call(account,{from: proj_config.address.private_net.TheBank }).valueOf();
balance = Blockchain.from_wei(balance,"ether");
// check if the balance of use is more than min_balance_required
if(balance <= min_bal_required)
return bot.sendMessage(fromId, `Sorry you don't have sufficient balance. Minimum balance needed is 0.25 ETH.`, { reply });
// insert the user account, waste type , a random_string to generate qr to transactons collection
var rand_for_qr = randomstring.generate(10);
var trans_object = prepareTransactionObject(rand_for_qr,account,waste_type);
trans_object.save(
function(err){
if(err)
console.log(err);
else
console.log('successful insertion: '+rand_for_qr);
});
// generate QR code with the random string
var qr = require('qr-image'),
qr_image_format = 'png',
qr_png = qr.image(rand_for_qr, { type: qr_image_format }),
qr_image_name = Date.now()+'.'+qr_image_format,
image_folder = './qr-images/',
qr_image_path = image_folder + qr_image_name,
write_stream = fs.createWriteStream(qr_image_path);
qr_png.pipe(write_stream);
write_stream.on('close',function(){
// send QR
bot.sendPhoto(fromId, qr_image_path , { fileName:qr_image_name });
// request location
var response_text = `Dispose. Waste type: `+ waste_type+`. Account balance: `+ balance+ ` Please send your location `;
var markup = bot.keyboard([[bot.button('location','Send location: ')]],"once");
return bot.sendMessage(fromId, response_text, { markup, ask: 'thrower_location' });
});
});
//listens to the location message of waste thrower
bot.on('ask.thrower_location', msg => {
// this location event is fired whenever a location is sent in the chat by the user
// to map the event with the appropriate chat, the chat id can be mapped.
const {latitude, longitude} = msg.location;
console.log(latitude+","+longitude);
let fromId = msg.from.id,
firstName = msg.from.first_name,
reply = msg.message_id,
query = getSGBLocationsQuery(latitude,longitude,km_radius);
// query the db for the closest SGB location where min_bal_required < user.balance
SGB.findOne(query).then(function(validSGB){
if(Object.keys(validSGB).length === 0 )
return bot.sendMessage(fromId, `Sorry there are no SGB in `+ km_radius +` km circle your location`, { reply });
//getting ETH to EUR conversion
//It will be displayed in the google map
request(proj_config.price_api,function(err,res,data){
var exchange = 0;
try{
exchange = JSON.parse(data).price.eur.toFixed(2);
}catch(e){
exchange = 45.50;
}
var map_url = "https://vast-falls-42691.herokuapp.com/maps/?lat="+validSGB.location.coordinates[0]+"&lon="+validSGB.location.coordinates[1]+"&clat="+latitude+"&clon="+longitude+"&percent_filled="+validSGB.percent_used+"&exchange="+exchange;
console.log(map_url);
return bot.sendMessage(fromId, map_url, { reply });
});
});
});
bot.on('/collect', msg => {
const id = msg.from.id;
// Ask user name
return bot.sendMessage(id, 'Please enter your Ethereum account: ', { ask: 'collect' });
});
// It will return all the sgb that are >= 50% filled. There are associated bounties on all the SGBs which is also displayed.
bot.on('ask.collect', msg => {
});
function getSGBLocationsQuery(latitude,longitude,radiusInKM){
var center = {"loc" : {
"type" : "Point",
"coordinates" : [
latitude,
longitude
]
}
}
// db.sgbs.findOne({"location": { $geoWithin : { $centerSphere : [59.9445047,30.2929776,0.00047088369172814315] } } })
var query = {
"location" : {
$geoWithin : {
$centerSphere : [center.loc.coordinates, kmToRadian(radiusInKM) ]
}
}
};
return query;
}
function kmToRadian(distanceInKM){
var earthRadiusInKM = 6371;
return distanceInKM / earthRadiusInKM;
}
function prepareTransactionObject(rand_for_qr,user_account,waste_type){
var trans_data = {
"user_account" : user_account,
"qr_code" : rand_for_qr,
"waste_type" : waste_type
};
return new Transaction(trans_data);
}
module.exports=bot;