-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontroller.js
More file actions
131 lines (116 loc) · 3.96 KB
/
Copy pathcontroller.js
File metadata and controls
131 lines (116 loc) · 3.96 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
var https = require('https');
var request = require('request');
var SECRET_TOKEN = process.env.DISTART_SECRET_TOKEN;
var TelegramBot = require('node-telegram-bot-api');
var bot = new TelegramBot(SECRET_TOKEN, {polling: false});
var fs = require('fs');
var file = fs.createWriteStream('./output');
module.exports = {
auth: function () {
return new Promise( function(resolve) {
console.log('la');
https.get('https://deepartapi.azurewebsites.net/create', function (r) {
streamToString(r, function(string) {
resolve(string);
})
})
})
},
sendMessage: sendMessage,
onImageDocReceived: function (token, document, user) {
var id = document.file_id;
getPhoto(token, id, user);
},
onImageReceived: function (token, photos, user) {
var id = photos[photos.length-1].file_id;
getPhoto(token, id, user);
},
onStart: function (token, users, username, params) {
console.log(token);
https.get('https://deepartapi.azurewebsites.net/start/' + token + '/300', function (r) {
sendMessage('Thanks! You will receive your image as soon as it is done', users[username].chatID);
moveUserToQueue (users, username);
});
}
}
function getPhoto (token, id, user) {
console.log('https://api.telegram.org/bot' + SECRET_TOKEN + '/getFile?file_id=' + id);
https.get('https://api.telegram.org/bot' + SECRET_TOKEN + '/getFile?file_id=' + id, function (res) {
res.on('data', function (d) {
console.log(JSON.parse(d.toString('utf8')));
var path = JSON.parse(d.toString('utf8')).result.file_path;
console.log('https://api.telegram.org/file/bot' + SECRET_TOKEN + '/' + path);
https.get('https://api.telegram.org/file/bot' + SECRET_TOKEN + '/' + path, function (r) {
r.pipe(request.post('https://deepartapi.azurewebsites.net/' + (user.content? 'content/' : 'style/') + token));
user.content++;
if (user.content == 1) {
sendMessage('Thanks! Please send the image on which you want to apply the style', user.chatID);
} else {
sendMessage('Thanks! Write start to start your job or delete to start again', user.chatID);
}
console.log("posted");
});
})
});
console.log(id);
}
function streamToString(s, cb) {
var string = ''
s.on('readable',function(buffer){
var part = s.read().toString();
string += part;
});
s.on('end',function(){
cb(string);
});
}
function sendMessage(text, chatID) {
console.log('https://api.telegram.org/bot' + SECRET_TOKEN + '/sendMessage');
request.post('https://api.telegram.org/bot' + SECRET_TOKEN + '/sendMessage', {form :{
text : text,
chat_id : chatID
}});
}
function moveUserToQueue (users, username) {
var user = users[username];
delete users[username];
queueListener(user);
}
function queueListener (user) {
//poll the status of the query every second
https.get('https://deepartapi.azurewebsites.net/status/' + user.token, function(r) {
streamToString(r, function(s) {
console.log(s);
if (s !== 'SUCCESS') { // iterate
setTimeout(function(){
queueListener(user);
}, 1000);
} else {
retrievePhoto(user);
}
});
});
}
function retrievePhoto(user) {
https.get('https://deepartapi.azurewebsites.net/get/' + user.token, function (r) {
sendPhoto(user, r);
});
}
function sendPhoto(user, photo) {
// bot.sendPhoto(chatId, photo, {caption: 'Lovely kittens'});
// console.log(photo);
// streamToString(photo, function(s){
// console.log(s);
// })
// file.pipe(photo);
sendMessage('https://deepartapi.azurewebsites.net/get/' + user.token, user.chatID);
// photo.setHeader('Content-Type', 'image/jpeg');
bot.sendPhoto(user.chatID, photo.toString());
// request.post('https://api.telegram.org/bot' + SECRET_TOKEN + '/sendPhoto', {form :{
// chat_id: chatID,
// photo: photo
// }}, function(e,r) {
// console.log('ah');
// console.log(e,r);
// });
}