-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathask.py
69 lines (58 loc) · 1.59 KB
/
ask.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
# -*- coding: utf-8 -*-
#
# TELEGRAM BOT
#
# Name: Nova
# Username: Nova_X1_Bot
#
# - Command: /ask
# - Description:
# Search Quora.com for an answer to query
#
# By Jose Acevedo
# Copyright 2016.
import random
import urllib
from bs4 import BeautifulSoup
# Specifies min and max length of text retrieved from the web to be sent as a message.
# Too small may retrieve undesired headers.
# Too big may cause problems with Telegram API.
MIN_LENGTH_FILTER=100
MAX_LENGTH_FILTER=800
notFoundAns = [
"Not found.",
"I couldn't find an answer to such a question...",
"That's not an easy one.",
"Not in my database. I need to collect more data from humans.",
"There are many things unknown in this universe. This is one of those.",
"Try again later :(",
"Try reformulating the question."
]
# Function to handle queries
def handleAsk(msg, chatID, bot):
if "reply_to_message" in msg:
query = msg["reply_to_message"]["text"]
else:
try:
query = msg["text"].split(' ',1)[1]
except:
query = ""
formattedQuery = ""
for i in range(len(query)):
if query[i] == ' ':
formattedQuery += '-'
else:
formattedQuery += query[i]
url = "https://www.quora.com/"+formattedQuery
html = urllib.urlopen(url).read()
soup = BeautifulSoup(html, "html.parser")
answer = ""
box=soup.findAll("span", attrs={"class":"rendered_qtext"})
for i in range(len(box)):
if MIN_LENGTH_FILTER < len(box[i].text) and len(box[i].text) < MAX_LENGTH_FILTER:
answer=box[i].text.strip()
break
# Results not found
if answer == "":
answer = random.choice(notFoundAns)
bot.sendMessage(chatID, answer)