bots: Move Google search bot and fix bugs.

Now this bot follows our latest structure for contrib_bots.

Switched the dependencies instructions, to install "google" rather than
"google-api-python-client".

Added all the search terms to the query (not only the first one).
This commit is contained in:
Yago González 2017-03-17 19:44:43 +00:00 committed by showell
parent 31b88c9225
commit 1894a134f8

View file

@ -3,13 +3,12 @@ from __future__ import print_function
import logging import logging
import http.client import http.client
from six.moves.urllib.request import urlopen from six.moves.urllib.request import urlopen
# Uses the Google search engine bindings
# pip install --upgrade google
from google import search from google import search
# Uses Google's Client Library def get_google_result(search_keywords):
# pip install --upgrade google-api-python-client
def get_google_result(original_content):
search_keywords = original_content.strip().split(' ', 1)[1]
if search_keywords == 'help': if search_keywords == 'help':
help_message = "To use this bot start message with @google \ help_message = "To use this bot start message with @google \
followed by what you want to search for. If \ followed by what you want to search for. If \
@ -29,7 +28,7 @@ def get_google_result(original_content):
return 'Error: Search failed. {}.'.format(e) return 'Error: Search failed. {}.'.format(e)
if not urls: if not urls:
return "No URLs returned by google." return 'No URLs returned by google.'
url = next(urls) url = next(urls)
@ -53,19 +52,6 @@ class GoogleSearchHandler(object):
should preface messages with @google. should preface messages with @google.
''' '''
def triage_message(self, message, client):
# return True if we want to (possibly) respond to this message
original_content = message['content']
# This next line of code is defensive, as we
# never want to get into an infinite loop of posting google
# search links.
if message['display_recipient'] == 'google':
return False
return original_content.startswith('@google')
def handle_message(self, message, client, state_handler): def handle_message(self, message, client, state_handler):
original_content = message['content'] original_content = message['content']
original_sender = message['sender_email'] original_sender = message['sender_email']
@ -90,7 +76,7 @@ handler_class = GoogleSearchHandler
def test(): def test():
try: try:
urlopen('http://216.58.192.142', timeout=1) urlopen('http://216.58.192.142', timeout=1)
print("Success") print('Success')
return True return True
except http.client.RemoteDisconnected as e: except http.client.RemoteDisconnected as e:
print('Error: {}'.format(e)) print('Error: {}'.format(e))