2017-05-23 20:01:44 -04:00
|
|
|
# See readme.md for instructions on running this code.
|
2016-12-21 13:27:01 -05:00
|
|
|
from __future__ import print_function
|
|
|
|
import logging
|
|
|
|
import http.client
|
|
|
|
from six.moves.urllib.request import urlopen
|
|
|
|
|
2017-03-17 15:44:43 -04:00
|
|
|
# Uses the Google search engine bindings
|
|
|
|
# pip install --upgrade google
|
|
|
|
from google import search
|
2016-12-21 13:27:01 -05:00
|
|
|
|
2017-05-23 20:01:44 -04:00
|
|
|
|
2017-03-17 15:44:43 -04:00
|
|
|
def get_google_result(search_keywords):
|
2017-05-23 20:01:44 -04:00
|
|
|
help_message = "To use this bot, start messages with @mentioned-bot, \
|
|
|
|
followed by what you want to search for. If \
|
|
|
|
found, Zulip will return the first search result \
|
|
|
|
on Google.\
|
|
|
|
\
|
|
|
|
An example message that could be sent is:\
|
|
|
|
'@mentioned-bot zulip' or \
|
|
|
|
'@mentioned-bot how to create a chatbot'."
|
2016-12-21 13:27:01 -05:00
|
|
|
if search_keywords == 'help':
|
2017-05-23 20:01:44 -04:00
|
|
|
return help_message
|
|
|
|
elif search_keywords == '' or search_keywords is None:
|
2016-12-21 13:27:01 -05:00
|
|
|
return help_message
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
urls = search(search_keywords, stop=20)
|
|
|
|
urlopen('http://216.58.192.142', timeout=1)
|
|
|
|
except http.client.RemoteDisconnected as er:
|
|
|
|
logging.exception(er)
|
|
|
|
return 'Error: No internet connection. {}.'.format(er)
|
|
|
|
except Exception as e:
|
|
|
|
logging.exception(e)
|
|
|
|
return 'Error: Search failed. {}.'.format(e)
|
|
|
|
|
2017-05-23 20:01:44 -04:00
|
|
|
try:
|
|
|
|
url = next(urls)
|
|
|
|
except AttributeError as a_err:
|
|
|
|
# google.search query failed and urls is of object
|
|
|
|
# 'NoneType'
|
|
|
|
logging.exception(a_err)
|
|
|
|
return "Error: Google search failed with a NoneType result. {}.".format(a_err)
|
|
|
|
except TypeError as t_err:
|
|
|
|
# google.search query failed and returned None
|
|
|
|
# This technically should not happen but the prior
|
|
|
|
# error check assumed this behavior
|
|
|
|
logging.exception(t_err)
|
|
|
|
return "Error: Google search function failed. {}.".format(t_err)
|
|
|
|
except Exception as e:
|
|
|
|
logging.exception(e)
|
|
|
|
return 'Error: Search failed. {}.'.format(e)
|
2016-12-21 13:27:01 -05:00
|
|
|
|
|
|
|
return 'Success: {}'.format(url)
|
|
|
|
|
2017-05-23 20:01:44 -04:00
|
|
|
|
2016-12-21 13:27:01 -05:00
|
|
|
class GoogleSearchHandler(object):
|
|
|
|
'''
|
|
|
|
This plugin allows users to enter a search
|
|
|
|
term in Zulip and get the top URL sent back
|
|
|
|
to the context (stream or private) in which
|
|
|
|
it was called. It looks for messages starting
|
2017-05-23 20:01:44 -04:00
|
|
|
with @mentioned-bot.
|
2016-12-21 13:27:01 -05:00
|
|
|
'''
|
|
|
|
|
|
|
|
def usage(self):
|
|
|
|
return '''
|
|
|
|
This plugin will allow users to search
|
|
|
|
for a given search term on Google from
|
2017-05-23 20:01:44 -04:00
|
|
|
Zulip. Use '@mentioned-bot help' to get
|
|
|
|
more information on the bot usage. Users
|
|
|
|
should preface messages with
|
|
|
|
@mentioned-bot.
|
2016-12-21 13:27:01 -05:00
|
|
|
'''
|
|
|
|
|
|
|
|
def handle_message(self, message, client, state_handler):
|
|
|
|
original_content = message['content']
|
|
|
|
result = get_google_result(original_content)
|
2017-05-24 15:49:44 -04:00
|
|
|
client.send_reply(message, result)
|
2016-12-21 13:27:01 -05:00
|
|
|
|
|
|
|
handler_class = GoogleSearchHandler
|
|
|
|
|
2017-05-23 20:01:44 -04:00
|
|
|
|
2016-12-21 13:27:01 -05:00
|
|
|
def test():
|
|
|
|
try:
|
|
|
|
urlopen('http://216.58.192.142', timeout=1)
|
2017-03-17 15:44:43 -04:00
|
|
|
print('Success')
|
2016-12-21 13:27:01 -05:00
|
|
|
return True
|
|
|
|
except http.client.RemoteDisconnected as e:
|
|
|
|
print('Error: {}'.format(e))
|
|
|
|
return False
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
test()
|