2016-12-13 05:28:20 +01:00
|
|
|
from __future__ import absolute_import
|
|
|
|
from __future__ import print_function
|
|
|
|
import requests
|
|
|
|
import logging
|
|
|
|
|
|
|
|
# See readme.md for instructions on running this code.
|
|
|
|
|
|
|
|
class WikipediaHandler(object):
|
|
|
|
'''
|
|
|
|
This plugin facilitates searching Wikipedia for a
|
|
|
|
specific key term and returns the top article from the
|
|
|
|
search. It looks for messages starting with '@wikipedia'
|
|
|
|
or '@wiki'.
|
|
|
|
|
|
|
|
In this example, we write all Wikipedia searches into
|
|
|
|
the same stream that it was called from, but this code
|
|
|
|
could be adapted to write Wikipedia searches to some
|
|
|
|
kind of external issue tracker as well.
|
|
|
|
'''
|
|
|
|
|
|
|
|
def usage(self):
|
|
|
|
return '''
|
|
|
|
This plugin will allow users to directly search
|
|
|
|
Wikipedia for a specific key term and get the top
|
|
|
|
article that is returned from the search. Users
|
|
|
|
should preface searches with "@wikipedia" or
|
|
|
|
"@wiki".
|
|
|
|
'''
|
|
|
|
|
2016-12-15 08:38:39 +01:00
|
|
|
def triage_message(self, message, client):
|
2016-12-13 05:28:20 +01:00
|
|
|
original_content = message['content']
|
|
|
|
|
|
|
|
# This next line of code is defensive, as we
|
|
|
|
# never want to get into an infinite loop of posting Wikipedia
|
|
|
|
# searches for own Wikipedia searches!
|
|
|
|
if message['sender_full_name'] == 'wikipedia-bot':
|
|
|
|
return False
|
|
|
|
is_wikipedia = (original_content.startswith('@wiki') or
|
|
|
|
original_content.startswith('@wikipedia'))
|
|
|
|
|
|
|
|
return is_wikipedia
|
|
|
|
|
|
|
|
def handle_message(self, message, client, state_handler):
|
2017-01-08 17:09:23 +01:00
|
|
|
query = message['content']
|
2016-12-13 05:28:20 +01:00
|
|
|
|
|
|
|
for prefix in ['@wikipedia', '@wiki']:
|
2017-01-08 17:09:23 +01:00
|
|
|
if query.startswith(prefix):
|
|
|
|
query = query[len(prefix)+1:]
|
2016-12-13 05:28:20 +01:00
|
|
|
break
|
|
|
|
|
2017-01-08 17:09:23 +01:00
|
|
|
query_wiki_link = ('https://en.wikipedia.org/w/api.php?action=query&'
|
|
|
|
'list=search&srsearch=%s&format=json' % (query,))
|
2016-12-13 05:28:20 +01:00
|
|
|
try:
|
2017-01-08 17:09:23 +01:00
|
|
|
data = requests.get(query_wiki_link)
|
|
|
|
except requests.exceptions.RequestException:
|
2016-12-13 05:28:20 +01:00
|
|
|
logging.error('broken link')
|
|
|
|
return
|
|
|
|
|
|
|
|
if data.status_code != 200:
|
|
|
|
logging.error('unsuccessful data')
|
|
|
|
return
|
|
|
|
|
2017-01-08 17:09:23 +01:00
|
|
|
new_content = 'For search term "' + query
|
2016-12-13 05:28:20 +01:00
|
|
|
if len(data.json()['query']['search']) == 0:
|
|
|
|
new_content = 'I am sorry. The search term you provided is not found :slightly_frowning_face:'
|
|
|
|
else:
|
2017-02-06 17:22:45 +01:00
|
|
|
search_string = data.json()['query']['search'][0]['title'].replace(' ', '_')
|
|
|
|
url = 'https://en.wikipedia.org/wiki/' + search_string
|
2016-12-13 05:28:20 +01:00
|
|
|
new_content = new_content + '", ' + url
|
|
|
|
|
|
|
|
client.send_message(dict(
|
|
|
|
type=message['type'],
|
|
|
|
to=message['display_recipient'],
|
|
|
|
subject=message['subject'],
|
|
|
|
content=new_content,
|
|
|
|
))
|
|
|
|
|
|
|
|
handler_class = WikipediaHandler
|