wikipedia bot: Return up to three links from wikipedia for keyword.
This change includes updates to the docs and tests as well.
This commit is contained in:
parent
2597de87ef
commit
f947ff44f8
|
@ -9,8 +9,15 @@
|
||||||
"query": {
|
"query": {
|
||||||
"search": [
|
"search": [
|
||||||
{
|
{
|
||||||
"title":"Sky blue"
|
"title":"Sky_blue"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"title":"Sky_Blue_Sky"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"title":"Blue_Sky"
|
||||||
}
|
}
|
||||||
|
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
|
@ -10,7 +10,13 @@
|
||||||
"search": [
|
"search": [
|
||||||
{
|
{
|
||||||
"title":"123"
|
"title":"123"
|
||||||
}
|
},
|
||||||
|
{
|
||||||
|
"title":"Japan_Airlines_Flight_123"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"title":"Iodine-123"
|
||||||
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
|
@ -10,6 +10,12 @@
|
||||||
"search": [
|
"search": [
|
||||||
{
|
{
|
||||||
"title":"Happiness"
|
"title":"Happiness"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"title":"Happy!"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"title":"Happy,_Happy"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,25 +9,39 @@ class TestWikipediaBot(StubBotTestCase):
|
||||||
|
|
||||||
# Single-word query
|
# Single-word query
|
||||||
bot_request = 'happy'
|
bot_request = 'happy'
|
||||||
bot_response = "For search term \"happy\", https://en.wikipedia.org/wiki/Happiness"
|
bot_response = ('''For search term:happy
|
||||||
|
1:[Happiness](https://en.wikipedia.org/wiki/Happiness)
|
||||||
|
2:[Happy!](https://en.wikipedia.org/wiki/Happy!)
|
||||||
|
3:[Happy,_Happy](https://en.wikipedia.org/wiki/Happy,_Happy)
|
||||||
|
''')
|
||||||
with self.mock_http_conversation('test_single_word'):
|
with self.mock_http_conversation('test_single_word'):
|
||||||
self.verify_reply(bot_request, bot_response)
|
self.verify_reply(bot_request, bot_response)
|
||||||
|
|
||||||
# Multi-word query
|
# Multi-word query
|
||||||
bot_request = 'The sky is blue'
|
bot_request = 'The sky is blue'
|
||||||
bot_response = "For search term \"The sky is blue\", https://en.wikipedia.org/wiki/Sky_blue"
|
bot_response = ('''For search term:The sky is blue
|
||||||
|
1:[Sky_blue](https://en.wikipedia.org/wiki/Sky_blue)
|
||||||
|
2:[Sky_Blue_Sky](https://en.wikipedia.org/wiki/Sky_Blue_Sky)
|
||||||
|
3:[Blue_Sky](https://en.wikipedia.org/wiki/Blue_Sky)
|
||||||
|
''')
|
||||||
with self.mock_http_conversation('test_multi_word'):
|
with self.mock_http_conversation('test_multi_word'):
|
||||||
self.verify_reply(bot_request, bot_response)
|
self.verify_reply(bot_request, bot_response)
|
||||||
|
|
||||||
# Number query
|
# Number query
|
||||||
bot_request = '123'
|
bot_request = '123'
|
||||||
bot_response = "For search term \"123\", https://en.wikipedia.org/wiki/123"
|
bot_response = ('''For search term:123
|
||||||
|
1:[123](https://en.wikipedia.org/wiki/123)
|
||||||
|
2:[Japan_Airlines_Flight_123](https://en.wikipedia.org/wiki/Japan_Airlines_Flight_123)
|
||||||
|
3:[Iodine-123](https://en.wikipedia.org/wiki/Iodine-123)
|
||||||
|
''')
|
||||||
with self.mock_http_conversation('test_number_query'):
|
with self.mock_http_conversation('test_number_query'):
|
||||||
self.verify_reply(bot_request, bot_response)
|
self.verify_reply(bot_request, bot_response)
|
||||||
|
|
||||||
# Hash query
|
# Hash query
|
||||||
bot_request = '#'
|
bot_request = '#'
|
||||||
bot_response = "For search term \"#\", https://en.wikipedia.org/wiki/Number_sign"
|
bot_response = '''For search term:#
|
||||||
|
1:[Number_sign](https://en.wikipedia.org/wiki/Number_sign)
|
||||||
|
'''
|
||||||
with self.mock_http_conversation('test_hash_query'):
|
with self.mock_http_conversation('test_hash_query'):
|
||||||
self.verify_reply(bot_request, bot_response)
|
self.verify_reply(bot_request, bot_response)
|
||||||
|
|
||||||
|
@ -39,5 +53,5 @@ class TestWikipediaBot(StubBotTestCase):
|
||||||
|
|
||||||
# Empty query, no request made to the Internet.
|
# Empty query, no request made to the Internet.
|
||||||
bot_request = ''
|
bot_request = ''
|
||||||
bot_response = "Please enter your message after @mention-bot"
|
bot_response = "Please enter your search term after @mention-bot"
|
||||||
self.verify_reply(bot_request, bot_response)
|
self.verify_reply(bot_request, bot_response)
|
||||||
|
|
|
@ -10,7 +10,7 @@ from six.moves import urllib
|
||||||
class WikipediaHandler(object):
|
class WikipediaHandler(object):
|
||||||
'''
|
'''
|
||||||
This plugin facilitates searching Wikipedia for a
|
This plugin facilitates searching Wikipedia for a
|
||||||
specific key term and returns the top article from the
|
specific key term and returns the top 3 articles from the
|
||||||
search. It looks for messages starting with '@mention-bot'
|
search. It looks for messages starting with '@mention-bot'
|
||||||
|
|
||||||
In this example, we write all Wikipedia searches into
|
In this example, we write all Wikipedia searches into
|
||||||
|
@ -21,47 +21,55 @@ class WikipediaHandler(object):
|
||||||
|
|
||||||
META = {
|
META = {
|
||||||
'name': 'Wikipedia',
|
'name': 'Wikipedia',
|
||||||
'description': 'Searches Wikipedia for a term and returns the top article.',
|
'description': 'Searches Wikipedia for a term and returns the top 3 articles.',
|
||||||
}
|
}
|
||||||
|
|
||||||
def usage(self):
|
def usage(self):
|
||||||
return '''
|
return '''
|
||||||
This plugin will allow users to directly search
|
This plugin will allow users to directly search
|
||||||
Wikipedia for a specific key term and get the top
|
Wikipedia for a specific key term and get the top 3
|
||||||
article that is returned from the search. Users
|
articles that is returned from the search. Users
|
||||||
should preface searches with "@mention-bot".
|
should preface searches with "@mention-bot".
|
||||||
'''
|
@mention-bot <name of article>'''
|
||||||
|
|
||||||
def handle_message(self, message, bot_handler):
|
def handle_message(self, message, bot_handler):
|
||||||
bot_response = self.get_bot_wiki_response(message, bot_handler)
|
bot_response = self.get_bot_wiki_response(message, bot_handler)
|
||||||
bot_handler.send_reply(message, bot_response)
|
bot_handler.send_reply(message, bot_response)
|
||||||
|
|
||||||
def get_bot_wiki_response(self, message, bot_handler):
|
def get_bot_wiki_response(self, message, bot_handler):
|
||||||
help_text = 'Please enter your message after @mention-bot'
|
'''This function returns the URLs of the requested topic.'''
|
||||||
|
|
||||||
|
help_text = 'Please enter your search term after @mention-bot'
|
||||||
|
|
||||||
|
# Checking if the link exists.
|
||||||
query = message['content']
|
query = message['content']
|
||||||
if query == '':
|
if query == '':
|
||||||
return help_text
|
return help_text
|
||||||
|
|
||||||
query_wiki_link = ('https://en.wikipedia.org/w/api.php?action=query&'
|
query_wiki_link = ('https://en.wikipedia.org/w/api.php?action=query&'
|
||||||
'list=search&srsearch=%s&format=json'
|
'list=search&srsearch=%s&format=json'
|
||||||
% (urllib.parse.quote(query),))
|
% (urllib.parse.quote(query),))
|
||||||
try:
|
try:
|
||||||
data = requests.get(query_wiki_link)
|
data = requests.get(query_wiki_link)
|
||||||
|
|
||||||
except requests.exceptions.RequestException:
|
except requests.exceptions.RequestException:
|
||||||
logging.error('broken link')
|
logging.error('broken link')
|
||||||
return
|
return
|
||||||
|
|
||||||
|
# Checking if the bot accessed the link.
|
||||||
if data.status_code != 200:
|
if data.status_code != 200:
|
||||||
logging.error('Page not found.')
|
logging.error('Page not found.')
|
||||||
return
|
return
|
||||||
|
new_content = 'For search term:' + query + '\n'
|
||||||
|
|
||||||
new_content = 'For search term "' + query
|
# Checking if there is content for the searched term
|
||||||
if len(data.json()['query']['search']) == 0:
|
if len(data.json()['query']['search']) == 0:
|
||||||
new_content = 'I am sorry. The search term you provided is not found :slightly_frowning_face:'
|
new_content = 'I am sorry. The search term you provided is not found :slightly_frowning_face:'
|
||||||
else:
|
else:
|
||||||
search_string = data.json()['query']['search'][0]['title'].replace(' ', '_')
|
for i in range(min(3, len(data.json()['query']['search']))):
|
||||||
url = 'https://en.wikipedia.org/wiki/' + search_string
|
search_string = data.json()['query']['search'][i]['title'].replace(' ', '_')
|
||||||
new_content = new_content + '", ' + url
|
url = 'https://en.wikipedia.org/wiki/' + search_string
|
||||||
|
new_content += str(i+1) + ':' + '[' + search_string + ']' + '(' + url.replace('"', "%22") + ')\n'
|
||||||
return new_content
|
return new_content
|
||||||
|
|
||||||
|
|
||||||
handler_class = WikipediaHandler
|
handler_class = WikipediaHandler
|
||||||
|
|
Loading…
Reference in a new issue