-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
341 lines (277 loc) · 8.6 KB
/
main.py
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
"""
@author Mr Suman Biswas
@github https://github.com/mrsumanbiswas
@Telegram_Bot MrDizzy_bot
"""
############ IMPORTS ##########
import os
import re
import json
import gtts
import random
import pymongo
import telebot
import requests
import wikipedia
import googlesearch
from time import strftime
from bs4 import BeautifulSoup
############ CODE #############
Type = 'cancel'
BOT_API = os.environ["BOT_API"]
MONGODB = os.environ["MONGODB"]
# DATABASE CONNECTION #####
"""
"""
# MONGODB CONNECTION
def dbConnect():
client = pymongo.MongoClient(host=MONGODB)
db = client['teleBot']
user = db['userDB']
history = db['chatDB']
collection = {
"user": user,
"history": history
}
return collection
# CLIENT DETAILS
def clientDetails(collection, time, chat_id, fullName, userName):
userExists = False
document = {
"time": time,
"chat_id": chat_id,
"fullName": fullName,
"userName": userName
}
for x in collection.find({"userName": userName}):
userExists = True
if not userExists:
collection.insert_one(document)
# CHAT HISTOY
def chatHistory(collection, time, chat_id, fullName, message):
doucment = {
"time": time,
"chat_id": chat_id,
"fullName": fullName,
"message": message
}
collection.insert_one(doucment)
# MODULE #####
"""
"""
# SEARCH ENGINE
def gSearch(quary: str):
data = "No reasult found ..."
try:
data = googlesearch.search(term=quary, num_results=20)
except Exception as e:
data = data + "\nError : " + e
return data
# IMAGE SCAPER
def imgScraping(quary):
"""
"""
ImgLink = []
u_agnt = {
'User-Agent':
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3',
'Accept-Encoding': 'none',
'Accept-Language': 'en-US,en;q=0.8',
'Connection': 'keep-alive',
}
search_url = 'https://www.google.com/search?site=&tbm=isch&source=hp&biw=1873&bih=990&q=' + quary
response = requests.get(search_url, headers=u_agnt)
html = response.text
b_soup = BeautifulSoup(html, 'html.parser')
results = b_soup.findAll('img', {'class': 'rg_i Q4LuWd'})
count = 0
for res in results:
try:
link = res['data-src']
ImgLink.append(link)
count = count + 1
if (count >= 15):
break
except KeyError:
continue
if ImgLink != []:
return ImgLink
# VACCINE SLOAT
def Vaccine(pincode):
date = str(int(strftime('%d'))+1) + str(strftime('-%m-%Y'))
try:
data = f"No Slot available for {date} in {pincode}"
api_request = f"https://cdn-api.co-vin.in/api/v2/appointment/sessions/public/findByPin?pincode={pincode}&date={date}" + \
"accept: application/json" + "Accept-Language: en_US"
response: list = json.loads(
requests.get(url=api_request).text
)['sessions']
if response != []:
for i in range(len(response)):
for key in response[i]:
value = response[i][f'{key}']
data += f"{key.replace('_',' ')} -> {value}\n\n"
return data
except Exception as e:
return "Error : " + str(e)
# WIKIPEDIA
def Wikipedia(quary: str) -> str:
try:
quary: str = quary.replace("summary:", "")
Summary = wikipedia.summary(quary, sentences=10)
texts2 = re.sub("\[.*?\]", "", Summary)
texts3 = re.sub("\(.*?\)", "", texts2)
Summary = texts3.replace(")", "").replace("(", "").replace("=", "")
return Summary
except:
return f"Sorry nothing match about : {quary}"
# LOGIC
class main_handler():
def __init__(self, bot, message) -> None:
"""
"""
self.bot = bot
self.message = message
msg = message.text.lower()
if Type == 'cancel':
self.text(
"This is a eco of your message ::\n"+message.text
)
self.audio(
self.tts(msg)
)
self.text(
"""\
/find --> search over internet
/about --> gives info in audio format
/pic --> gives photos form web
/vaccine --> gives Indian vaccination slot info
/details --> gives your telegram info
/dizzy --> gives info about the bot
"""
)
elif Type == 'find':
for x in gSearch(msg):
self.text(x)
elif Type == 'about':
txt = Wikipedia(msg)
self.text(txt)
self.audio(
path=self.tts(txt)
)
elif Type == 'pic':
for pic in imgScraping(msg):
self.image(pic)
elif Type == 'vaccine':
if len(msg) == 6:
self.text(Vaccine(msg))
else:
self.text("Please provide a valid (Indian) pincode")
def text(self, text: str):
self.bot.send_message(
chat_id=self.message.chat.id,
text=text
)
def audio(self, path: str):
self.bot.send_audio(
chat_id=self.message.chat.id,
audio=open(f'{path}', 'rb'))
os.system(f'rm -r {path}')
def image(self, path: str):
self.bot.send_photo(
self.message.chat.id,
photo=path
)
def tts(self, text: str):
fileName = random.randint(a=1, b=10)
fileName = str(fileName) + ".mp3"
try:
gtts.gTTS(
text=text
).save(
savefile=fileName
)
return fileName
except Exception as e:
text("Error : " + e)
# EXECUTION
def main():
bot = telebot.TeleBot(token=BOT_API)
@bot.message_handler(commands=['start'])
def commands(message):
chat_id = message.chat.id
fullName = f'{message.from_user.first_name} ' + \
f'{message.from_user.last_name}'
userName = message.from_user.username
clientDetails(
collection=dbConnect()['user'],
time=strftime('%H:%M:%S || %d.%m.%Y || %z'),
chat_id=chat_id,
fullName=fullName,
userName=userName
)
bot.send_message(
chat_id,
text=f"Welcome {fullName}.\nI'm Mr Dizzy here for you."
)
@bot.message_handler(commands=['details'])
def commands(message):
bot.send_message(
message.chat.id,
text=f"""\
First Name : {message.from_user.first_name}
Last Name : {message.from_user.last_name}
User Name : @{message.from_user.username}"""
)
@bot.message_handler(commands=['dizzy'])
def commands(message):
bot.send_message(
message.chat.id,
text="Not now I shall tell you letter...\nI'm a bot..."
)
@bot.message_handler(commands=['find', 'about', 'pic', 'vaccine', 'cancel'])
def commands(message):
global Type
Type = message.text.replace('/', '')
replys = {
'find': 'What do you want to find ? \nI shall bring that to you.',
'about': 'What information do you want?\nI shall give you a mp3 file with that.',
'pic': 'Let me know what picture you want.\nI shall try my best.',
'vaccine': "Please provide me the zipcode/pincode (Indian).\nI shall show you slots available for next day.",
'cancel': 'Alright!',
}
text = replys[Type]
if Type != "cancel":
text += '\nIf noting then send /cancel'
bot.send_message(
message.chat.id,
text=text
)
@bot.message_handler()
def handler(message):
global Type
chat_id = message.chat.id
fullName = f'{message.from_user.first_name} ' + \
f'{message.from_user.last_name}'
if Type != 'cancel':
Message = Type + ' : ' + message.text
else:
Message = message.text
chatHistory(
collection=dbConnect()['history'],
time=strftime('%H:%M:%S || %d.%m.%Y || %z'),
chat_id=chat_id,
fullName=fullName,
message=Message
)
try:
main_handler(bot, message)
Type = "cancel"
except Exception as e:
bot.reply_to(message, "err: " + str(e))
bot.polling(non_stop=True, skip_pending=True)
########### Runner ##############
if __name__ == "__main__":
main()