-
Notifications
You must be signed in to change notification settings - Fork 1
/
bot.js
151 lines (126 loc) · 4.37 KB
/
bot.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
const token = process.env.TOKEN;
const request = require('request');
const Bot = require('node-telegram-bot-api');
var https = require('https');
let bot;
if(process.env.NODE_ENV === 'production') {
bot = new Bot(token);
bot.setWebHook(process.env.HEROKU_URL + bot.token);
}
else {
bot = new Bot(token, { polling: true });
}
console.log('Bot server started in the ' + process.env.NODE_ENV + ' mode');
bot.onText(/\/start/, (msg) => {
bot.sendMessage(msg.chat.id, "Welcome!");
});
bot.onText(/\/imageof/,(msg) => {
bot.sendMessage(msg.chat.id,"Enter Search Term for Image",{reply_to_message_id:msg.message_id,reply_markup:{"force_reply":true}});
});
string_to_array = function (str) {
return str.trim().split(" ");
};
function callback(results,id,msgid,gsr) {
var temp=Math.floor(Math.random()*results.length);
if(results.length>0){
var url=results[temp];
// console.log(url);
if(url.link==undefined){
bot.sendMessage(id,'Error',{reply_to_message_id:msgid});
}
var count=(url.link.match(/www/g)|| []).length;
if(url.image.contextLink.includes('facebook')||url.image.contextLink.includes('youtube')||url.image.contextLink.includes('twitter')){
url=results[temp].image.thumbnailLink;
}
else if(count>=2){
var index=nthIndex(url.link,'www',2);
url='https://'+url.link.toString().slice(index);
}
else{
url=url.link;
}
try{
bot.sendPhoto(id,url,{parse_mode:'Markdown',caption:'[Google Image Search Results]('+gsr+')', reply_to_message_id:msgid});
} catch(error) {
console.error(error)
}
}
else{
bot.sendMessage(id,'Image not found',{reply_to_message_id:msgid});
}
}
function nthIndex(str, pat, n){
var L= str.length, i= -1;
while(n-- && i++<L){
i= str.indexOf(pat, i);
if (i < 0) break;
}
return i;
}
bot.on('message', (msg) => {
var hi="hi";
var bye = "bye";
const name = msg.from.first_name;
const username = msg.from.username;
if(msg.text.toString().toLowerCase().indexOf(hi)===0||msg.text.toString().toLowerCase().indexOf('hey')==0||msg.text.toString().toLowerCase().indexOf('hello')==0){
const reply='Hello, ' + name + ' (@'+username+')!';
bot.sendMessage(msg.chat.id, reply,{reply_to_message_id:msg.message_id}).then(() => {
// reply sent!
});
}
else if (msg.text.toString().toLowerCase().includes(bye)) {
bot.sendMessage(msg.chat.id,"Goodbye, "+name+' (@'+username+')!',{reply_to_message_id:msg.message_id});
}
if(msg.reply_to_message!=undefined){
if((msg.reply_to_message.from.id==791119811)&&(msg.reply_to_message.text.toString().toLowerCase()=='enter search term for image')){
var searchTerm=msg.text.toString();
var temp=string_to_array(searchTerm);
var gsr='https://www.google.co.in/search?q='+temp[0];
not=temp.length;
if(not>1){
for(var i=1;i<not;i++){
gsr=gsr+'+'+temp[i];
}
}
gsr+='&tbm=isch&source=lnms&sa=X&ved=0ahUKEwjurPut1cLeAhWDF3IKHT1JDm0Q_AUICygC&biw=1853&bih=953&dpr=1'
var results = getImageSearchResults(searchTerm, callback, 0, 10,msg.chat.id,msg.message_id,gsr);
}}
});
function getImageSearchResults(searchTerm, callback, start, num,id,msgid,gsr) {
start = start < 0 || start > 90 || typeof(start) === 'undefined' ? 0 : start;
num = num < 1 || num > 10 || typeof(num) === 'undefined' ? 10 : num;
if (!searchTerm) {
console.error('No search term');
}
var parameters = '&q=' + encodeURIComponent(searchTerm);
parameters += '&searchType=image';
parameters += start ? '&start=' + start : '';
parameters += '&num=' + num;
var options = {
host: 'www.googleapis.com',
path: '/customsearch/v1?key=' + process.env.CSE_API_KEY + '&cx=' + process.env.CSE_ID + parameters
};
var result = '';
https.get(options, function(response) {
response.setEncoding('utf8');
response.on('data', function(data) {
result += data;
});
response.on('end', function () {
var data = JSON.parse(result);
var resultsArray = [];
if(data.error && data.error.errors) {
resultsArray.push(data.error.errors[0]);
callback(resultsArray,id,msgid,gsr);
} else if(data.items) {
data.items.forEach(function (item) {
resultsArray.push(item);
});
callback(resultsArray,id,msgid,gsr);
} else {
callback([],id,msgid,gsr);
}
});
});
}
module.exports = bot;