2016-12-12 23:28:20 -05:00
|
|
|
from __future__ import absolute_import
|
|
|
|
from __future__ import print_function
|
|
|
|
import requests
|
|
|
|
import logging
|
2017-03-05 13:58:18 -05:00
|
|
|
import re
|
2016-12-12 23:28:20 -05:00
|
|
|
|
|
|
|
# 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
|
2017-02-16 16:56:32 -05:00
|
|
|
search. It looks for messages starting with '@mention-bot'
|
2016-12-12 23:28:20 -05:00
|
|
|
|
|
|
|
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
|
2017-02-16 16:56:32 -05:00
|
|
|
should preface searches with "@mention-bot".
|
2016-12-12 23:28:20 -05:00
|
|
|
'''
|
|
|
|
|
|
|
|
def handle_message(self, message, client, state_handler):
|
2017-03-05 13:58:18 -05:00
|
|
|
bot_response = self.get_bot_wiki_response(message, client)
|
|
|
|
if message['type'] == 'private':
|
|
|
|
client.send_message(dict(
|
|
|
|
type='private',
|
|
|
|
to=message['sender_email'],
|
|
|
|
content=bot_response,
|
|
|
|
))
|
|
|
|
else:
|
|
|
|
client.send_message(dict(
|
|
|
|
type='stream',
|
|
|
|
to=message['display_recipient'],
|
|
|
|
subject=message['subject'],
|
|
|
|
content=bot_response,
|
|
|
|
))
|
2016-12-12 23:28:20 -05:00
|
|
|
|
2017-03-05 13:58:18 -05:00
|
|
|
def get_bot_wiki_response(self, message, client):
|
|
|
|
query = message['content']
|
2017-01-08 11:09:23 -05:00
|
|
|
query_wiki_link = ('https://en.wikipedia.org/w/api.php?action=query&'
|
|
|
|
'list=search&srsearch=%s&format=json' % (query,))
|
2016-12-12 23:28:20 -05:00
|
|
|
try:
|
2017-01-08 11:09:23 -05:00
|
|
|
data = requests.get(query_wiki_link)
|
|
|
|
except requests.exceptions.RequestException:
|
2016-12-12 23:28:20 -05:00
|
|
|
logging.error('broken link')
|
|
|
|
return
|
|
|
|
|
|
|
|
if data.status_code != 200:
|
|
|
|
logging.error('unsuccessful data')
|
|
|
|
return
|
|
|
|
|
2017-01-08 11:09:23 -05:00
|
|
|
new_content = 'For search term "' + query
|
2016-12-12 23:28:20 -05: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 11:22:45 -05:00
|
|
|
search_string = data.json()['query']['search'][0]['title'].replace(' ', '_')
|
|
|
|
url = 'https://en.wikipedia.org/wiki/' + search_string
|
2016-12-12 23:28:20 -05:00
|
|
|
new_content = new_content + '", ' + url
|
2017-03-05 13:58:18 -05:00
|
|
|
return new_content
|
2016-12-12 23:28:20 -05:00
|
|
|
|
|
|
|
|
|
|
|
handler_class = WikipediaHandler
|