bots: Move contrib_bots to api/bots*.
This will make it convenient to include these bots in Zulip API releases on pypi. Fix #5009.
This commit is contained in:
		
							parent
							
								
									7531c4fb26
								
							
						
					
					
						commit
						894adb1e43
					
				
					 110 changed files with 36 additions and 27 deletions
				
			
		
							
								
								
									
										0
									
								
								bots/wikipedia/__init__.py
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										0
									
								
								bots/wikipedia/__init__.py
									
										
									
									
									
										Normal file
									
								
							
							
								
								
									
										31
									
								
								bots/wikipedia/test_wikipedia.py
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										31
									
								
								bots/wikipedia/test_wikipedia.py
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,31 @@
 | 
			
		|||
#!/usr/bin/env python
 | 
			
		||||
 | 
			
		||||
from __future__ import absolute_import
 | 
			
		||||
from __future__ import print_function
 | 
			
		||||
 | 
			
		||||
import os
 | 
			
		||||
import sys
 | 
			
		||||
 | 
			
		||||
our_dir = os.path.dirname(os.path.abspath(__file__))
 | 
			
		||||
sys.path.insert(0, os.path.normpath(os.path.join(our_dir)))
 | 
			
		||||
# For dev setups, we can find the API in the repo itself.
 | 
			
		||||
if os.path.exists(os.path.join(our_dir, '..')):
 | 
			
		||||
    sys.path.insert(0, '..')
 | 
			
		||||
from bots_test_lib import BotTestCase
 | 
			
		||||
 | 
			
		||||
class TestWikipediaBot(BotTestCase):
 | 
			
		||||
    bot_name = "wikipedia"
 | 
			
		||||
 | 
			
		||||
    def test_bot(self):
 | 
			
		||||
        expected = {
 | 
			
		||||
            "": 'Please enter your message after @mention-bot',
 | 
			
		||||
            "sssssss kkkkk": ('I am sorry. The search term you provided '
 | 
			
		||||
                              'is not found :slightly_frowning_face:'),
 | 
			
		||||
            "foo": ('For search term "foo", '
 | 
			
		||||
                    'https://en.wikipedia.org/wiki/Foobar'),
 | 
			
		||||
            "123": ('For search term "123", '
 | 
			
		||||
                    'https://en.wikipedia.org/wiki/123'),
 | 
			
		||||
            "laugh": ('For search term "laugh", '
 | 
			
		||||
                      'https://en.wikipedia.org/wiki/Laughter'),
 | 
			
		||||
        }
 | 
			
		||||
        self.check_expected_responses(expected)
 | 
			
		||||
							
								
								
									
										60
									
								
								bots/wikipedia/wikipedia.py
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										60
									
								
								bots/wikipedia/wikipedia.py
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,60 @@
 | 
			
		|||
from __future__ import absolute_import
 | 
			
		||||
from __future__ import print_function
 | 
			
		||||
import requests
 | 
			
		||||
import logging
 | 
			
		||||
import re
 | 
			
		||||
 | 
			
		||||
# 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 '@mention-bot'
 | 
			
		||||
 | 
			
		||||
    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 "@mention-bot".
 | 
			
		||||
            '''
 | 
			
		||||
 | 
			
		||||
    def handle_message(self, message, client, state_handler):
 | 
			
		||||
        bot_response = self.get_bot_wiki_response(message, client)
 | 
			
		||||
        client.send_reply(message, bot_response)
 | 
			
		||||
 | 
			
		||||
    def get_bot_wiki_response(self, message, client):
 | 
			
		||||
        help_text = 'Please enter your message after @mention-bot'
 | 
			
		||||
        query = message['content']
 | 
			
		||||
        if query == '':
 | 
			
		||||
            return help_text
 | 
			
		||||
        query_wiki_link = ('https://en.wikipedia.org/w/api.php?action=query&'
 | 
			
		||||
                           'list=search&srsearch=%s&format=json' % (query,))
 | 
			
		||||
        try:
 | 
			
		||||
            data = requests.get(query_wiki_link)
 | 
			
		||||
        except requests.exceptions.RequestException:
 | 
			
		||||
            logging.error('broken link')
 | 
			
		||||
            return
 | 
			
		||||
 | 
			
		||||
        if data.status_code != 200:
 | 
			
		||||
            logging.error('unsuccessful data')
 | 
			
		||||
            return
 | 
			
		||||
 | 
			
		||||
        new_content = 'For search term "' + query
 | 
			
		||||
        if len(data.json()['query']['search']) == 0:
 | 
			
		||||
            new_content = 'I am sorry. The search term you provided is not found :slightly_frowning_face:'
 | 
			
		||||
        else:
 | 
			
		||||
            search_string = data.json()['query']['search'][0]['title'].replace(' ', '_')
 | 
			
		||||
            url = 'https://en.wikipedia.org/wiki/' + search_string
 | 
			
		||||
            new_content = new_content + '", ' + url
 | 
			
		||||
        return new_content
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
handler_class = WikipediaHandler
 | 
			
		||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue