bots: Fix googlesearch bot exceptions and add readme.
This commit is contained in:
		
							parent
							
								
									bb70c5beed
								
							
						
					
					
						commit
						a8b825388d
					
				
					 2 changed files with 58 additions and 14 deletions
				
			
		| 
						 | 
					@ -1,4 +1,4 @@
 | 
				
			||||||
# See zulip/contrib_bots/lib/readme.md for instructions on running this code.
 | 
					# See readme.md for instructions on running this code.
 | 
				
			||||||
from __future__ import print_function
 | 
					from __future__ import print_function
 | 
				
			||||||
import logging
 | 
					import logging
 | 
				
			||||||
import http.client
 | 
					import http.client
 | 
				
			||||||
| 
						 | 
					@ -8,13 +8,19 @@ from six.moves.urllib.request import urlopen
 | 
				
			||||||
#   pip install --upgrade google
 | 
					#   pip install --upgrade google
 | 
				
			||||||
from google import search
 | 
					from google import search
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def get_google_result(search_keywords):
 | 
					def get_google_result(search_keywords):
 | 
				
			||||||
 | 
					    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'."
 | 
				
			||||||
    if search_keywords == 'help':
 | 
					    if search_keywords == 'help':
 | 
				
			||||||
        help_message = "To use this bot start message with @google \
 | 
					        return help_message
 | 
				
			||||||
                        followed by what you want to search for. If \
 | 
					    elif search_keywords == '' or search_keywords is None:
 | 
				
			||||||
                        found, Zulip will return the first search result \
 | 
					 | 
				
			||||||
                        on Google. An example message that could be sent is:\
 | 
					 | 
				
			||||||
                        '@google zulip' or '@google how to create a chatbot'."
 | 
					 | 
				
			||||||
        return help_message
 | 
					        return help_message
 | 
				
			||||||
    else:
 | 
					    else:
 | 
				
			||||||
        try:
 | 
					        try:
 | 
				
			||||||
| 
						 | 
					@ -27,29 +33,43 @@ def get_google_result(search_keywords):
 | 
				
			||||||
            logging.exception(e)
 | 
					            logging.exception(e)
 | 
				
			||||||
            return 'Error: Search failed. {}.'.format(e)
 | 
					            return 'Error: Search failed. {}.'.format(e)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if not urls:
 | 
					        try:
 | 
				
			||||||
            return 'No URLs returned by google.'
 | 
					            url = next(urls)
 | 
				
			||||||
 | 
					        except AttributeError as a_err:
 | 
				
			||||||
        url = next(urls)
 | 
					            # 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)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        return 'Success: {}'.format(url)
 | 
					        return 'Success: {}'.format(url)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class GoogleSearchHandler(object):
 | 
					class GoogleSearchHandler(object):
 | 
				
			||||||
    '''
 | 
					    '''
 | 
				
			||||||
    This plugin allows users to enter a search
 | 
					    This plugin allows users to enter a search
 | 
				
			||||||
    term in Zulip and get the top URL sent back
 | 
					    term in Zulip and get the top URL sent back
 | 
				
			||||||
    to the context (stream or private) in which
 | 
					    to the context (stream or private) in which
 | 
				
			||||||
    it was called. It looks for messages starting
 | 
					    it was called. It looks for messages starting
 | 
				
			||||||
    with @google.
 | 
					    with @mentioned-bot.
 | 
				
			||||||
    '''
 | 
					    '''
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def usage(self):
 | 
					    def usage(self):
 | 
				
			||||||
        return '''
 | 
					        return '''
 | 
				
			||||||
            This plugin will allow users to search
 | 
					            This plugin will allow users to search
 | 
				
			||||||
            for a given search term on Google from
 | 
					            for a given search term on Google from
 | 
				
			||||||
            Zulip. Use '@google help' to get more
 | 
					            Zulip. Use '@mentioned-bot help' to get
 | 
				
			||||||
            information on the bot usage. Users
 | 
					            more information on the bot usage. Users
 | 
				
			||||||
            should preface messages with @google.
 | 
					            should preface messages with
 | 
				
			||||||
 | 
					            @mentioned-bot.
 | 
				
			||||||
            '''
 | 
					            '''
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def handle_message(self, message, client, state_handler):
 | 
					    def handle_message(self, message, client, state_handler):
 | 
				
			||||||
| 
						 | 
					@ -73,6 +93,7 @@ class GoogleSearchHandler(object):
 | 
				
			||||||
 | 
					
 | 
				
			||||||
handler_class = GoogleSearchHandler
 | 
					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)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										23
									
								
								contrib_bots/bots/googlesearch/readme.md
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										23
									
								
								contrib_bots/bots/googlesearch/readme.md
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,23 @@
 | 
				
			||||||
 | 
					# Google Search bot
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					This bot allows users to do Google search queries and have the bot
 | 
				
			||||||
 | 
					respond with the first search result.  It is by default set to the
 | 
				
			||||||
 | 
					highest safe-search setting.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					## Usage
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					Run this bot as described
 | 
				
			||||||
 | 
					[here](http://zulip.readthedocs.io/en/latest/bots-guide.html#how-to-deploy-a-bot).
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					Use this bot with the following command
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					`@mentioned-bot <search terms>`
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					This will return the first link found by Google for `<search terms>`
 | 
				
			||||||
 | 
					and print the resulting URL.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					If no `<search terms>` are entered, a help message is printed instead.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					If there was an error in the process of running the search (socket
 | 
				
			||||||
 | 
					errors, Google search function failed, or general failures), an error
 | 
				
			||||||
 | 
					message is returned.
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue