mirror of
				https://github.com/zulip/zulip.git
				synced 2025-11-04 05:53:43 +00:00 
			
		
		
		
	bots: Rename BotHandlerApi object client to bot_handler.
				
					
				
			This commit is contained in:
		@@ -89,8 +89,8 @@ class CommuteHandler(object):
 | 
				
			|||||||
            return config.get('Google.com', 'api_key')
 | 
					            return config.get('Google.com', 'api_key')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    # determines if bot will respond as a private message/ stream message
 | 
					    # determines if bot will respond as a private message/ stream message
 | 
				
			||||||
    def send_info(self, message, letter, client):
 | 
					    def send_info(self, message, letter, bot_handler):
 | 
				
			||||||
        client.send_reply(message, letter)
 | 
					        bot_handler.send_reply(message, letter)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def calculate_seconds(self, time_str):
 | 
					    def calculate_seconds(self, time_str):
 | 
				
			||||||
        times = time_str.split(',')
 | 
					        times = time_str.split(',')
 | 
				
			||||||
@@ -114,7 +114,7 @@ class CommuteHandler(object):
 | 
				
			|||||||
        return
 | 
					        return
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    # gets content for output and sends it to user
 | 
					    # gets content for output and sends it to user
 | 
				
			||||||
    def get_send_content(self, rjson, params, message, client):
 | 
					    def get_send_content(self, rjson, params, message, bot_handler):
 | 
				
			||||||
        try:
 | 
					        try:
 | 
				
			||||||
            # JSON list of output variables
 | 
					            # JSON list of output variables
 | 
				
			||||||
            variable_list = rjson["rows"][0]["elements"][0]
 | 
					            variable_list = rjson["rows"][0]["elements"][0]
 | 
				
			||||||
@@ -126,14 +126,14 @@ class CommuteHandler(object):
 | 
				
			|||||||
            if no_result:
 | 
					            if no_result:
 | 
				
			||||||
                self.send_info(message,
 | 
					                self.send_info(message,
 | 
				
			||||||
                               "Zero results\nIf stuck, try '@commute help'.",
 | 
					                               "Zero results\nIf stuck, try '@commute help'.",
 | 
				
			||||||
                               client)
 | 
					                               bot_handler)
 | 
				
			||||||
                return
 | 
					                return
 | 
				
			||||||
            elif not_found or invalid_request:
 | 
					            elif not_found or invalid_request:
 | 
				
			||||||
                raise IndexError
 | 
					                raise IndexError
 | 
				
			||||||
        except IndexError:
 | 
					        except IndexError:
 | 
				
			||||||
            self.send_info(message,
 | 
					            self.send_info(message,
 | 
				
			||||||
                           "Invalid input, please see instructions."
 | 
					                           "Invalid input, please see instructions."
 | 
				
			||||||
                           "\nIf stuck, try '@commute help'.", client)
 | 
					                           "\nIf stuck, try '@commute help'.", bot_handler)
 | 
				
			||||||
            return
 | 
					            return
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        # origin and destination strings
 | 
					        # origin and destination strings
 | 
				
			||||||
@@ -165,7 +165,7 @@ class CommuteHandler(object):
 | 
				
			|||||||
            output += '\n' + duration
 | 
					            output += '\n' + duration
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        # bot sends commute information to user
 | 
					        # bot sends commute information to user
 | 
				
			||||||
        self.send_info(message, output, client)
 | 
					        self.send_info(message, output, bot_handler)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    # creates parameters for HTTP request
 | 
					    # creates parameters for HTTP request
 | 
				
			||||||
    def parse_pair(self, content_list):
 | 
					    def parse_pair(self, content_list):
 | 
				
			||||||
@@ -180,7 +180,7 @@ class CommuteHandler(object):
 | 
				
			|||||||
            result[key] = value
 | 
					            result[key] = value
 | 
				
			||||||
        return result
 | 
					        return result
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def receive_response(self, params, message, client):
 | 
					    def receive_response(self, params, message, bot_handler):
 | 
				
			||||||
        def validate_requests(request):
 | 
					        def validate_requests(request):
 | 
				
			||||||
            if request.status_code == 200:
 | 
					            if request.status_code == 200:
 | 
				
			||||||
                return request.json()
 | 
					                return request.json()
 | 
				
			||||||
@@ -189,30 +189,30 @@ class CommuteHandler(object):
 | 
				
			|||||||
                               "Something went wrong. Please try again." +
 | 
					                               "Something went wrong. Please try again." +
 | 
				
			||||||
                               " Error: {error_num}.\n{error_text}"
 | 
					                               " Error: {error_num}.\n{error_text}"
 | 
				
			||||||
                               .format(error_num=request.status_code,
 | 
					                               .format(error_num=request.status_code,
 | 
				
			||||||
                                       error_text=request.text), client)
 | 
					                                       error_text=request.text), bot_handler)
 | 
				
			||||||
                return
 | 
					                return
 | 
				
			||||||
        r = requests.get('https://maps.googleapis.com/maps/api/' +
 | 
					        r = requests.get('https://maps.googleapis.com/maps/api/' +
 | 
				
			||||||
                         'distancematrix/json', params=params)
 | 
					                         'distancematrix/json', params=params)
 | 
				
			||||||
        result = validate_requests(r)
 | 
					        result = validate_requests(r)
 | 
				
			||||||
        return result
 | 
					        return result
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def handle_message(self, message, client, state_handler):
 | 
					    def handle_message(self, message, bot_handler, state_handler):
 | 
				
			||||||
        original_content = message['content']
 | 
					        original_content = message['content']
 | 
				
			||||||
        query = original_content.split()
 | 
					        query = original_content.split()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if "help" in query:
 | 
					        if "help" in query:
 | 
				
			||||||
            self.send_info(message, self.help_info, client)
 | 
					            self.send_info(message, self.help_info, bot_handler)
 | 
				
			||||||
            return
 | 
					            return
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        params = self.parse_pair(query)
 | 
					        params = self.parse_pair(query)
 | 
				
			||||||
        params['key'] = self.api_key
 | 
					        params['key'] = self.api_key
 | 
				
			||||||
        self.add_time_to_params(params)
 | 
					        self.add_time_to_params(params)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        rjson = self.receive_response(params, message, client)
 | 
					        rjson = self.receive_response(params, message, bot_handler)
 | 
				
			||||||
        if not rjson:
 | 
					        if not rjson:
 | 
				
			||||||
            return
 | 
					            return
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        self.get_send_content(rjson, params, message, client)
 | 
					        self.get_send_content(rjson, params, message, bot_handler)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
handler_class = CommuteHandler
 | 
					handler_class = CommuteHandler
 | 
				
			||||||
handler = CommuteHandler()
 | 
					handler = CommuteHandler()
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -47,11 +47,11 @@ class ConverterHandler(object):
 | 
				
			|||||||
               all supported units.
 | 
					               all supported units.
 | 
				
			||||||
               '''
 | 
					               '''
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def handle_message(self, message, client, state_handler):
 | 
					    def handle_message(self, message, bot_handler, state_handler):
 | 
				
			||||||
        bot_response = get_bot_converter_response(message, client)
 | 
					        bot_response = get_bot_converter_response(message, bot_handler)
 | 
				
			||||||
        client.send_reply(message, bot_response)
 | 
					        bot_handler.send_reply(message, bot_response)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def get_bot_converter_response(message, client):
 | 
					def get_bot_converter_response(message, bot_handler):
 | 
				
			||||||
    content = message['content']
 | 
					    content = message['content']
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    words = content.lower().split()
 | 
					    words = content.lower().split()
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -21,11 +21,11 @@ class DefineHandler(object):
 | 
				
			|||||||
            messages with @mention-bot.
 | 
					            messages with @mention-bot.
 | 
				
			||||||
            '''
 | 
					            '''
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def handle_message(self, message, client, state_handler):
 | 
					    def handle_message(self, message, bot_handler, state_handler):
 | 
				
			||||||
        original_content = message['content'].strip()
 | 
					        original_content = message['content'].strip()
 | 
				
			||||||
        bot_response = self.get_bot_define_response(original_content)
 | 
					        bot_response = self.get_bot_define_response(original_content)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        client.send_reply(message, bot_response)
 | 
					        bot_handler.send_reply(message, bot_response)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def get_bot_define_response(self, original_content):
 | 
					    def get_bot_define_response(self, original_content):
 | 
				
			||||||
        split_content = original_content.split(' ')
 | 
					        split_content = original_content.split(' ')
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -28,9 +28,9 @@ class EncryptHandler(object):
 | 
				
			|||||||
            Feeding encrypted messages into the bot decrypts them.
 | 
					            Feeding encrypted messages into the bot decrypts them.
 | 
				
			||||||
            '''
 | 
					            '''
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def handle_message(self, message, client, state_handler):
 | 
					    def handle_message(self, message, bot_handler, state_handler):
 | 
				
			||||||
        bot_response = self.get_bot_encrypt_response(message)
 | 
					        bot_response = self.get_bot_encrypt_response(message)
 | 
				
			||||||
        client.send_reply(message, bot_response)
 | 
					        bot_handler.send_reply(message, bot_response)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def get_bot_encrypt_response(self, message):
 | 
					    def get_bot_encrypt_response(self, message):
 | 
				
			||||||
        original_content = message['content']
 | 
					        original_content = message['content']
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -22,13 +22,13 @@ class FollowupHandler(object):
 | 
				
			|||||||
            called "followup" that your API user can send to.
 | 
					            called "followup" that your API user can send to.
 | 
				
			||||||
            '''
 | 
					            '''
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def handle_message(self, message, client, state_handler):
 | 
					    def handle_message(self, message, bot_handler, state_handler):
 | 
				
			||||||
        if message['content'] == '':
 | 
					        if message['content'] == '':
 | 
				
			||||||
            bot_response = "Please specify the message you want to send to followup stream after @mention-bot"
 | 
					            bot_response = "Please specify the message you want to send to followup stream after @mention-bot"
 | 
				
			||||||
            client.send_reply(message, bot_response)
 | 
					            bot_handler.send_reply(message, bot_response)
 | 
				
			||||||
        else:
 | 
					        else:
 | 
				
			||||||
            bot_response = self.get_bot_followup_response(message)
 | 
					            bot_response = self.get_bot_followup_response(message)
 | 
				
			||||||
            client.send_message(dict(
 | 
					            bot_handler.send_message(dict(
 | 
				
			||||||
                type='stream',
 | 
					                type='stream',
 | 
				
			||||||
                to='followup',
 | 
					                to='followup',
 | 
				
			||||||
                subject=message['sender_email'],
 | 
					                subject=message['sender_email'],
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -59,13 +59,13 @@ Example Inputs:
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        return '\n'.join(format_venue(venue) for venue in venues)
 | 
					        return '\n'.join(format_venue(venue) for venue in venues)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def send_info(self, message, letter, client):
 | 
					    def send_info(self, message, letter, bot_handler):
 | 
				
			||||||
        client.send_reply(message, letter)
 | 
					        bot_handler.send_reply(message, letter)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def handle_message(self, message, client, state_handler):
 | 
					    def handle_message(self, message, bot_handler, state_handler):
 | 
				
			||||||
        words = message['content'].split()
 | 
					        words = message['content'].split()
 | 
				
			||||||
        if "/help" in words:
 | 
					        if "/help" in words:
 | 
				
			||||||
            self.send_info(message, self.help_info, client)
 | 
					            self.send_info(message, self.help_info, bot_handler)
 | 
				
			||||||
            return
 | 
					            return
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        # These are required inputs for the HTTP request.
 | 
					        # These are required inputs for the HTTP request.
 | 
				
			||||||
@@ -96,19 +96,19 @@ Example Inputs:
 | 
				
			|||||||
        else:
 | 
					        else:
 | 
				
			||||||
            self.send_info(message,
 | 
					            self.send_info(message,
 | 
				
			||||||
                           "Invalid Request\nIf stuck, try '@mention-bot help'.",
 | 
					                           "Invalid Request\nIf stuck, try '@mention-bot help'.",
 | 
				
			||||||
                           client)
 | 
					                           bot_handler)
 | 
				
			||||||
            return
 | 
					            return
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if received_json['meta']['code'] == 200:
 | 
					        if received_json['meta']['code'] == 200:
 | 
				
			||||||
            response_msg = ('Food nearby ' + params['near'] +
 | 
					            response_msg = ('Food nearby ' + params['near'] +
 | 
				
			||||||
                            ' coming right up:\n' +
 | 
					                            ' coming right up:\n' +
 | 
				
			||||||
                            self.format_json(received_json['response']['venues']))
 | 
					                            self.format_json(received_json['response']['venues']))
 | 
				
			||||||
            self.send_info(message, response_msg, client)
 | 
					            self.send_info(message, response_msg, bot_handler)
 | 
				
			||||||
            return
 | 
					            return
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        self.send_info(message,
 | 
					        self.send_info(message,
 | 
				
			||||||
                       "Invalid Request\nIf stuck, try '@mention-bot help'.",
 | 
					                       "Invalid Request\nIf stuck, try '@mention-bot help'.",
 | 
				
			||||||
                       client)
 | 
					                       bot_handler)
 | 
				
			||||||
        return
 | 
					        return
 | 
				
			||||||
 | 
					
 | 
				
			||||||
handler_class = FoursquareHandler
 | 
					handler_class = FoursquareHandler
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -33,9 +33,9 @@ class GiphyHandler(object):
 | 
				
			|||||||
            The bot responds also to private messages.
 | 
					            The bot responds also to private messages.
 | 
				
			||||||
            '''
 | 
					            '''
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def handle_message(self, message, client, state_handler):
 | 
					    def handle_message(self, message, bot_handler, state_handler):
 | 
				
			||||||
        bot_response = get_bot_giphy_response(message, client)
 | 
					        bot_response = get_bot_giphy_response(message, bot_handler)
 | 
				
			||||||
        client.send_reply(message, bot_response)
 | 
					        bot_handler.send_reply(message, bot_response)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class GiphyNoResultException(Exception):
 | 
					class GiphyNoResultException(Exception):
 | 
				
			||||||
@@ -73,7 +73,7 @@ def get_url_gif_giphy(keyword, api_key):
 | 
				
			|||||||
    return gif_url
 | 
					    return gif_url
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def get_bot_giphy_response(message, client):
 | 
					def get_bot_giphy_response(message, bot_handler):
 | 
				
			||||||
    # Each exception has a specific reply should "gif_url" return a number.
 | 
					    # Each exception has a specific reply should "gif_url" return a number.
 | 
				
			||||||
    # The bot will post the appropriate message for the error.
 | 
					    # The bot will post the appropriate message for the error.
 | 
				
			||||||
    keyword = message['content']
 | 
					    keyword = message['content']
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -39,11 +39,11 @@ class GitHubHandler(object):
 | 
				
			|||||||
            '<repository_owner>/<repository>/<issue_number>/<your_comment>'.
 | 
					            '<repository_owner>/<repository>/<issue_number>/<your_comment>'.
 | 
				
			||||||
            '''
 | 
					            '''
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def handle_message(self, message, client, state_handler):
 | 
					    def handle_message(self, message, bot_handler, state_handler):
 | 
				
			||||||
        original_content = message['content']
 | 
					        original_content = message['content']
 | 
				
			||||||
        original_sender = message['sender_email']
 | 
					        original_sender = message['sender_email']
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        handle_input(client, original_content, original_sender)
 | 
					        handle_input(bot_handler, original_content, original_sender)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
handler_class = GitHubHandler
 | 
					handler_class = GitHubHandler
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -77,7 +77,7 @@ def get_values_message(original_content):
 | 
				
			|||||||
        raise InputError
 | 
					        raise InputError
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def handle_input(client, original_content, original_sender):
 | 
					def handle_input(bot_handler, original_content, original_sender):
 | 
				
			||||||
    try:
 | 
					    try:
 | 
				
			||||||
        params = get_values_message(original_content)
 | 
					        params = get_values_message(original_content)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -89,7 +89,7 @@ def handle_input(client, original_content, original_sender):
 | 
				
			|||||||
            reply_message = "You commented on issue number " + params['issue'] + " under " + \
 | 
					            reply_message = "You commented on issue number " + params['issue'] + " under " + \
 | 
				
			||||||
                            params['repo_owner'] + "'s repository " + params['repo'] + "!"
 | 
					                            params['repo_owner'] + "'s repository " + params['repo'] + "!"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            send_message(client, reply_message, original_sender)
 | 
					            send_message(bot_handler, reply_message, original_sender)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        elif status_code == 404:
 | 
					        elif status_code == 404:
 | 
				
			||||||
            # this error could be from an error with the OAuth token
 | 
					            # this error could be from an error with the OAuth token
 | 
				
			||||||
@@ -98,7 +98,7 @@ def handle_input(client, original_content, original_sender):
 | 
				
			|||||||
                            params['repo_owner'] + "'s repository " + params['repo'] + \
 | 
					                            params['repo_owner'] + "'s repository " + params['repo'] + \
 | 
				
			||||||
                            ". Do you have the right OAuth token?"
 | 
					                            ". Do you have the right OAuth token?"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            send_message(client, reply_message, original_sender)
 | 
					            send_message(bot_handler, reply_message, original_sender)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        else:
 | 
					        else:
 | 
				
			||||||
            # sending info to github did not work
 | 
					            # sending info to github did not work
 | 
				
			||||||
@@ -108,18 +108,18 @@ def handle_input(client, original_content, original_sender):
 | 
				
			|||||||
                            params['repo_owner'] + "'s repository " + params['repo'] + \
 | 
					                            params['repo_owner'] + "'s repository " + params['repo'] + \
 | 
				
			||||||
                            ". Did you enter the information in the correct format?"
 | 
					                            ". Did you enter the information in the correct format?"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            send_message(client, reply_message, original_sender)
 | 
					            send_message(bot_handler, reply_message, original_sender)
 | 
				
			||||||
    except InputError:
 | 
					    except InputError:
 | 
				
			||||||
            message = "It doesn't look like the information was entered in the correct format." \
 | 
					            message = "It doesn't look like the information was entered in the correct format." \
 | 
				
			||||||
                      " Did you input it like this? " \
 | 
					                      " Did you input it like this? " \
 | 
				
			||||||
                      "'/<username>/<repository_owner>/<repository>/<issue_number>/<your_comment>'."
 | 
					                      "'/<username>/<repository_owner>/<repository>/<issue_number>/<your_comment>'."
 | 
				
			||||||
            send_message(client, message, original_sender)
 | 
					            send_message(bot_handler, message, original_sender)
 | 
				
			||||||
            logging.error('there was an error with the information you entered')
 | 
					            logging.error('there was an error with the information you entered')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def send_message(client, message, original_sender):
 | 
					def send_message(bot_handler, message, original_sender):
 | 
				
			||||||
    # function for sending a message
 | 
					    # function for sending a message
 | 
				
			||||||
    client.send_message(dict(
 | 
					    bot_handler.send_message(dict(
 | 
				
			||||||
        type='private',
 | 
					        type='private',
 | 
				
			||||||
        to=original_sender,
 | 
					        to=original_sender,
 | 
				
			||||||
        content=message,
 | 
					        content=message,
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -47,7 +47,7 @@ class IssueHandler(object):
 | 
				
			|||||||
            github_token = <oauth_token>   (The personal access token for the GitHub bot)
 | 
					            github_token = <oauth_token>   (The personal access token for the GitHub bot)
 | 
				
			||||||
            '''
 | 
					            '''
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def handle_message(self, message, client, state_handler):
 | 
					    def handle_message(self, message, bot_handler, state_handler):
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        original_content = message['content']
 | 
					        original_content = message['content']
 | 
				
			||||||
        original_sender = message['sender_email']
 | 
					        original_sender = message['sender_email']
 | 
				
			||||||
@@ -86,7 +86,7 @@ class IssueHandler(object):
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        if r.ok:
 | 
					        if r.ok:
 | 
				
			||||||
            # sends the message onto the 'issues' stream so it can be seen by zulip users
 | 
					            # sends the message onto the 'issues' stream so it can be seen by zulip users
 | 
				
			||||||
            client.send_message(dict(
 | 
					            bot_handler.send_message(dict(
 | 
				
			||||||
                type='stream',
 | 
					                type='stream',
 | 
				
			||||||
                to='issues',
 | 
					                to='issues',
 | 
				
			||||||
                subject=message['sender_email'],
 | 
					                subject=message['sender_email'],
 | 
				
			||||||
@@ -96,7 +96,7 @@ class IssueHandler(object):
 | 
				
			|||||||
            return
 | 
					            return
 | 
				
			||||||
            # This means that the issue has not been sent
 | 
					            # This means that the issue has not been sent
 | 
				
			||||||
            # sends the message onto the 'issues' stream so it can be seen by zulip users
 | 
					            # sends the message onto the 'issues' stream so it can be seen by zulip users
 | 
				
			||||||
            client.send_message(dict(
 | 
					            bot_handler.send_message(dict(
 | 
				
			||||||
                type='stream',
 | 
					                type='stream',
 | 
				
			||||||
                to='issues',
 | 
					                to='issues',
 | 
				
			||||||
                subject=message['sender_email'],
 | 
					                subject=message['sender_email'],
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,7 +1,7 @@
 | 
				
			|||||||
# See 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.bot_handler
 | 
				
			||||||
from six.moves.urllib.request import urlopen
 | 
					from six.moves.urllib.request import urlopen
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# Uses the Google search engine bindings
 | 
					# Uses the Google search engine bindings
 | 
				
			||||||
@@ -26,7 +26,7 @@ def get_google_result(search_keywords):
 | 
				
			|||||||
        try:
 | 
					        try:
 | 
				
			||||||
            urls = search(search_keywords, stop=20)
 | 
					            urls = search(search_keywords, stop=20)
 | 
				
			||||||
            urlopen('http://216.58.192.142', timeout=1)
 | 
					            urlopen('http://216.58.192.142', timeout=1)
 | 
				
			||||||
        except http.client.RemoteDisconnected as er:
 | 
					        except http.bot_handler.RemoteDisconnected as er:
 | 
				
			||||||
            logging.exception(er)
 | 
					            logging.exception(er)
 | 
				
			||||||
            return 'Error: No internet connection. {}.'.format(er)
 | 
					            return 'Error: No internet connection. {}.'.format(er)
 | 
				
			||||||
        except Exception as e:
 | 
					        except Exception as e:
 | 
				
			||||||
@@ -72,10 +72,10 @@ class GoogleSearchHandler(object):
 | 
				
			|||||||
            @mentioned-bot.
 | 
					            @mentioned-bot.
 | 
				
			||||||
            '''
 | 
					            '''
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def handle_message(self, message, client, state_handler):
 | 
					    def handle_message(self, message, bot_handler, state_handler):
 | 
				
			||||||
        original_content = message['content']
 | 
					        original_content = message['content']
 | 
				
			||||||
        result = get_google_result(original_content)
 | 
					        result = get_google_result(original_content)
 | 
				
			||||||
        client.send_reply(message, result)
 | 
					        bot_handler.send_reply(message, result)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
handler_class = GoogleSearchHandler
 | 
					handler_class = GoogleSearchHandler
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -85,7 +85,7 @@ def test():
 | 
				
			|||||||
        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.bot_handler.RemoteDisconnected as e:
 | 
				
			||||||
        print('Error: {}'.format(e))
 | 
					        print('Error: {}'.format(e))
 | 
				
			||||||
        return False
 | 
					        return False
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -11,8 +11,8 @@ class HelloWorldHandler(object):
 | 
				
			|||||||
        sophisticated, bots.
 | 
					        sophisticated, bots.
 | 
				
			||||||
        '''
 | 
					        '''
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def handle_message(self, message, client, state_handler):
 | 
					    def handle_message(self, message, bot_handler, state_handler):
 | 
				
			||||||
        content = 'beep boop'
 | 
					        content = 'beep boop'
 | 
				
			||||||
        client.send_reply(message, content)
 | 
					        bot_handler.send_reply(message, content)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
handler_class = HelloWorldHandler
 | 
					handler_class = HelloWorldHandler
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -11,8 +11,8 @@ class HelpHandler(object):
 | 
				
			|||||||
            your Zulip instance.
 | 
					            your Zulip instance.
 | 
				
			||||||
            '''
 | 
					            '''
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def handle_message(self, message, client, state_handler):
 | 
					    def handle_message(self, message, bot_handler, state_handler):
 | 
				
			||||||
        help_content = "Info on Zulip can be found here:\nhttps://github.com/zulip/zulip"
 | 
					        help_content = "Info on Zulip can be found here:\nhttps://github.com/zulip/zulip"
 | 
				
			||||||
        client.send_reply(message, help_content)
 | 
					        bot_handler.send_reply(message, help_content)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
handler_class = HelpHandler
 | 
					handler_class = HelpHandler
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -81,11 +81,11 @@ class HowdoiHandler(object):
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        return answer
 | 
					        return answer
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def handle_message(self, message, client, state_handler):
 | 
					    def handle_message(self, message, bot_handler, state_handler):
 | 
				
			||||||
        question = message['content'].strip()
 | 
					        question = message['content'].strip()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if question.startswith('howdowe!'):
 | 
					        if question.startswith('howdowe!'):
 | 
				
			||||||
            client.send_message(dict(
 | 
					            bot_handler.send_message(dict(
 | 
				
			||||||
                type='stream',
 | 
					                type='stream',
 | 
				
			||||||
                to=message['display_recipient'],
 | 
					                to=message['display_recipient'],
 | 
				
			||||||
                subject=message['subject'],
 | 
					                subject=message['subject'],
 | 
				
			||||||
@@ -93,14 +93,14 @@ class HowdoiHandler(object):
 | 
				
			|||||||
            ))
 | 
					            ))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        elif question.startswith('howdoi!'):
 | 
					        elif question.startswith('howdoi!'):
 | 
				
			||||||
            client.send_message(dict(
 | 
					            bot_handler.send_message(dict(
 | 
				
			||||||
                type='private',
 | 
					                type='private',
 | 
				
			||||||
                to=message['sender_email'],
 | 
					                to=message['sender_email'],
 | 
				
			||||||
                content=self.get_answer('howdoi!', question)
 | 
					                content=self.get_answer('howdoi!', question)
 | 
				
			||||||
            ))
 | 
					            ))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        elif question.startswith('howdowe'):
 | 
					        elif question.startswith('howdowe'):
 | 
				
			||||||
            client.send_message(dict(
 | 
					            bot_handler.send_message(dict(
 | 
				
			||||||
                type='stream',
 | 
					                type='stream',
 | 
				
			||||||
                to=message['display_recipient'],
 | 
					                to=message['display_recipient'],
 | 
				
			||||||
                subject=message['subject'],
 | 
					                subject=message['subject'],
 | 
				
			||||||
@@ -108,7 +108,7 @@ class HowdoiHandler(object):
 | 
				
			|||||||
            ))
 | 
					            ))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        elif question.startswith('howdoi'):
 | 
					        elif question.startswith('howdoi'):
 | 
				
			||||||
            client.send_message(dict(
 | 
					            bot_handler.send_message(dict(
 | 
				
			||||||
                type='private',
 | 
					                type='private',
 | 
				
			||||||
                to=message['sender_email'],
 | 
					                to=message['sender_email'],
 | 
				
			||||||
                content=self.get_answer('howdoi', question)
 | 
					                content=self.get_answer('howdoi', question)
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -15,13 +15,13 @@ class IncrementorHandler(object):
 | 
				
			|||||||
        is @-mentioned, this number will be incremented in the same message.
 | 
					        is @-mentioned, this number will be incremented in the same message.
 | 
				
			||||||
        '''
 | 
					        '''
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def handle_message(self, message, client, state_handler):
 | 
					    def handle_message(self, message, bot_handler, state_handler):
 | 
				
			||||||
        self.number += 1
 | 
					        self.number += 1
 | 
				
			||||||
        if self.message_id is None:
 | 
					        if self.message_id is None:
 | 
				
			||||||
            result = client.send_reply(message, str(self.number))
 | 
					            result = bot_handler.send_reply(message, str(self.number))
 | 
				
			||||||
            self.message_id = result['id']
 | 
					            self.message_id = result['id']
 | 
				
			||||||
        else:
 | 
					        else:
 | 
				
			||||||
            client.update_message(dict(
 | 
					            bot_handler.update_message(dict(
 | 
				
			||||||
                message_id=self.message_id,
 | 
					                message_id=self.message_id,
 | 
				
			||||||
                content=str(self.number),
 | 
					                content=str(self.number),
 | 
				
			||||||
            ))
 | 
					            ))
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -115,9 +115,9 @@ class JohnHandler(object):
 | 
				
			|||||||
            mantain a conversation, joke and give useful information.
 | 
					            mantain a conversation, joke and give useful information.
 | 
				
			||||||
            '''
 | 
					            '''
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def handle_message(self, message, client, state_handler):
 | 
					    def handle_message(self, message, bot_handler, state_handler):
 | 
				
			||||||
        original_content = message['content']
 | 
					        original_content = message['content']
 | 
				
			||||||
        bot_response = str(bota.get_response(original_content))
 | 
					        bot_response = str(bota.get_response(original_content))
 | 
				
			||||||
        client.send_reply(message, bot_response)
 | 
					        bot_handler.send_reply(message, bot_response)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
handler_class = JohnHandler
 | 
					handler_class = JohnHandler
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -63,9 +63,9 @@ class ThesaurusHandler(object):
 | 
				
			|||||||
            preface messages with @mention-bot synonym or @mention-bot antonym.
 | 
					            preface messages with @mention-bot synonym or @mention-bot antonym.
 | 
				
			||||||
            '''
 | 
					            '''
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def handle_message(self, message, client, state_handler):
 | 
					    def handle_message(self, message, bot_handler, state_handler):
 | 
				
			||||||
        original_content = message['content'].strip()
 | 
					        original_content = message['content'].strip()
 | 
				
			||||||
        new_content = get_thesaurus_result(original_content)
 | 
					        new_content = get_thesaurus_result(original_content)
 | 
				
			||||||
        client.send_reply(message, new_content)
 | 
					        bot_handler.send_reply(message, new_content)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
handler_class = ThesaurusHandler
 | 
					handler_class = ThesaurusHandler
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -274,7 +274,7 @@ class ticTacToeHandler(object):
 | 
				
			|||||||
            message starts with @mention-bot.
 | 
					            message starts with @mention-bot.
 | 
				
			||||||
            '''
 | 
					            '''
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def handle_message(self, message, client, state_handler):
 | 
					    def handle_message(self, message, bot_handler, state_handler):
 | 
				
			||||||
        command_list = message['content']
 | 
					        command_list = message['content']
 | 
				
			||||||
        command = ""
 | 
					        command = ""
 | 
				
			||||||
        for val in command_list:
 | 
					        for val in command_list:
 | 
				
			||||||
@@ -313,7 +313,7 @@ class ticTacToeHandler(object):
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        state_handler.set_state(mydict)
 | 
					        state_handler.set_state(mydict)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        client.send_message(dict(
 | 
					        bot_handler.send_message(dict(
 | 
				
			||||||
            type = 'private',
 | 
					            type = 'private',
 | 
				
			||||||
            to = original_sender,
 | 
					            to = original_sender,
 | 
				
			||||||
            subject = message['sender_email'],
 | 
					            subject = message['sender_email'],
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -7,7 +7,7 @@ class VirtualFsHandler(object):
 | 
				
			|||||||
    def usage(self):
 | 
					    def usage(self):
 | 
				
			||||||
        return get_help()
 | 
					        return get_help()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def handle_message(self, message, client, state_handler):
 | 
					    def handle_message(self, message, bot_handler, state_handler):
 | 
				
			||||||
        command = message['content']
 | 
					        command = message['content']
 | 
				
			||||||
        if command == "":
 | 
					        if command == "":
 | 
				
			||||||
            command = "help"
 | 
					            command = "help"
 | 
				
			||||||
@@ -32,7 +32,7 @@ class VirtualFsHandler(object):
 | 
				
			|||||||
        state[recipient] = fs
 | 
					        state[recipient] = fs
 | 
				
			||||||
        state_handler.set_state(state)
 | 
					        state_handler.set_state(state)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        client.send_reply(message, msg)
 | 
					        bot_handler.send_reply(message, msg)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def get_help():
 | 
					def get_help():
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -23,7 +23,7 @@ class WeatherHandler(object):
 | 
				
			|||||||
            This plugin will give info about weather in a specified city
 | 
					            This plugin will give info about weather in a specified city
 | 
				
			||||||
            '''
 | 
					            '''
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def handle_message(self, message, client, state_handler):
 | 
					    def handle_message(self, message, bot_handler, state_handler):
 | 
				
			||||||
        help_content = '''
 | 
					        help_content = '''
 | 
				
			||||||
            This bot returns weather info for specified city.
 | 
					            This bot returns weather info for specified city.
 | 
				
			||||||
            You specify city in the following format:
 | 
					            You specify city in the following format:
 | 
				
			||||||
@@ -44,7 +44,7 @@ class WeatherHandler(object):
 | 
				
			|||||||
            else:
 | 
					            else:
 | 
				
			||||||
                response = format_response(r.text, message['content'], self.response_pattern)
 | 
					                response = format_response(r.text, message['content'], self.response_pattern)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        client.send_reply(message, response)
 | 
					        bot_handler.send_reply(message, response)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def format_response(text, city, response_pattern):
 | 
					def format_response(text, city, response_pattern):
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -26,11 +26,11 @@ class WikipediaHandler(object):
 | 
				
			|||||||
            should preface searches with "@mention-bot".
 | 
					            should preface searches with "@mention-bot".
 | 
				
			||||||
            '''
 | 
					            '''
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def handle_message(self, message, client, state_handler):
 | 
					    def handle_message(self, message, bot_handler, state_handler):
 | 
				
			||||||
        bot_response = self.get_bot_wiki_response(message, client)
 | 
					        bot_response = self.get_bot_wiki_response(message, bot_handler)
 | 
				
			||||||
        client.send_reply(message, bot_response)
 | 
					        bot_handler.send_reply(message, bot_response)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def get_bot_wiki_response(self, message, client):
 | 
					    def get_bot_wiki_response(self, message, bot_handler):
 | 
				
			||||||
        help_text = 'Please enter your message after @mention-bot'
 | 
					        help_text = 'Please enter your message after @mention-bot'
 | 
				
			||||||
        query = message['content']
 | 
					        query = message['content']
 | 
				
			||||||
        if query == '':
 | 
					        if query == '':
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -27,9 +27,9 @@ class XkcdHandler(object):
 | 
				
			|||||||
            `<comic_id>`, e.g `@mention-bot 1234`.
 | 
					            `<comic_id>`, e.g `@mention-bot 1234`.
 | 
				
			||||||
            '''
 | 
					            '''
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def handle_message(self, message, client, state_handler):
 | 
					    def handle_message(self, message, bot_handler, state_handler):
 | 
				
			||||||
        xkcd_bot_response = get_xkcd_bot_response(message)
 | 
					        xkcd_bot_response = get_xkcd_bot_response(message)
 | 
				
			||||||
        client.send_reply(message, xkcd_bot_response)
 | 
					        bot_handler.send_reply(message, xkcd_bot_response)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class XkcdBotCommand(object):
 | 
					class XkcdBotCommand(object):
 | 
				
			||||||
    LATEST = 0
 | 
					    LATEST = 0
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -49,8 +49,8 @@ class YodaSpeakHandler(object):
 | 
				
			|||||||
            @mention-bot You will learn how to speak like me someday.
 | 
					            @mention-bot You will learn how to speak like me someday.
 | 
				
			||||||
            '''
 | 
					            '''
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def handle_message(self, message, client, state_handler):
 | 
					    def handle_message(self, message, bot_handler, state_handler):
 | 
				
			||||||
        handle_input(message, client)
 | 
					        handle_input(message, bot_handler)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
handler_class = YodaSpeakHandler
 | 
					handler_class = YodaSpeakHandler
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -86,11 +86,11 @@ def format_input(original_content):
 | 
				
			|||||||
    return sentence
 | 
					    return sentence
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def handle_input(message, client):
 | 
					def handle_input(message, bot_handler):
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    original_content = message['content']
 | 
					    original_content = message['content']
 | 
				
			||||||
    if is_help(original_content):
 | 
					    if is_help(original_content):
 | 
				
			||||||
        client.send_reply(message, HELP_MESSAGE)
 | 
					        bot_handler.send_reply(message, HELP_MESSAGE)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    else:
 | 
					    else:
 | 
				
			||||||
        sentence = format_input(original_content)
 | 
					        sentence = format_input(original_content)
 | 
				
			||||||
@@ -106,7 +106,7 @@ def handle_input(message, client):
 | 
				
			|||||||
                            '`readme.md` file?'
 | 
					                            '`readme.md` file?'
 | 
				
			||||||
            logging.error(reply_message)
 | 
					            logging.error(reply_message)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        client.send_reply(message, reply_message)
 | 
					        bot_handler.send_reply(message, reply_message)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def get_api_key():
 | 
					def get_api_key():
 | 
				
			||||||
@@ -117,9 +117,9 @@ def get_api_key():
 | 
				
			|||||||
    return api_key
 | 
					    return api_key
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def send_message(client, message, stream, subject):
 | 
					def send_message(bot_handler, message, stream, subject):
 | 
				
			||||||
    # function for sending a message
 | 
					    # function for sending a message
 | 
				
			||||||
    client.send_message(dict(
 | 
					    bot_handler.send_message(dict(
 | 
				
			||||||
        type='stream',
 | 
					        type='stream',
 | 
				
			||||||
        to=stream,
 | 
					        to=stream,
 | 
				
			||||||
        subject=subject,
 | 
					        subject=subject,
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -8,14 +8,14 @@ class YoutubeHandler(object):
 | 
				
			|||||||
            This bot will return the first Youtube search result for the give query.
 | 
					            This bot will return the first Youtube search result for the give query.
 | 
				
			||||||
            '''
 | 
					            '''
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def handle_message(self, message, client, state_handler):
 | 
					    def handle_message(self, message, bot_handler, state_handler):
 | 
				
			||||||
        help_content = '''
 | 
					        help_content = '''
 | 
				
			||||||
            To use the, Youtube Bot send `@mention-bot search terms`
 | 
					            To use the, Youtube Bot send `@mention-bot search terms`
 | 
				
			||||||
            Example:
 | 
					            Example:
 | 
				
			||||||
            @mention-bot funny cats
 | 
					            @mention-bot funny cats
 | 
				
			||||||
            '''.strip()
 | 
					            '''.strip()
 | 
				
			||||||
        if message['content'] == '':
 | 
					        if message['content'] == '':
 | 
				
			||||||
            client.send_reply(message, help_content)
 | 
					            bot_handler.send_reply(message, help_content)
 | 
				
			||||||
        else:
 | 
					        else:
 | 
				
			||||||
            text_to_search = message['content']
 | 
					            text_to_search = message['content']
 | 
				
			||||||
            url = "https://www.youtube.com/results?search_query=" + text_to_search
 | 
					            url = "https://www.youtube.com/results?search_query=" + text_to_search
 | 
				
			||||||
@@ -24,8 +24,8 @@ class YoutubeHandler(object):
 | 
				
			|||||||
            video_id = soup.find(attrs={'class': 'yt-uix-tile-link'})
 | 
					            video_id = soup.find(attrs={'class': 'yt-uix-tile-link'})
 | 
				
			||||||
            try:
 | 
					            try:
 | 
				
			||||||
                link = 'https://www.youtube.com' + video_id['href']
 | 
					                link = 'https://www.youtube.com' + video_id['href']
 | 
				
			||||||
                client.send_reply(message, link)
 | 
					                bot_handler.send_reply(message, link)
 | 
				
			||||||
            except TypeError:
 | 
					            except TypeError:
 | 
				
			||||||
                client.send_reply(message, 'No video found for specified search terms')
 | 
					                bot_handler.send_reply(message, 'No video found for specified search terms')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
handler_class = YoutubeHandler
 | 
					handler_class = YoutubeHandler
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -167,7 +167,7 @@ def run_message_handler_for_bot(lib_module, quiet, config_file):
 | 
				
			|||||||
        if is_private_message or is_mentioned:
 | 
					        if is_private_message or is_mentioned:
 | 
				
			||||||
            message_handler.handle_message(
 | 
					            message_handler.handle_message(
 | 
				
			||||||
                message=message,
 | 
					                message=message,
 | 
				
			||||||
                client=restricted_client,
 | 
					                bot_handler=restricted_client,
 | 
				
			||||||
                state_handler=state_handler
 | 
					                state_handler=state_handler
 | 
				
			||||||
            )
 | 
					            )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -150,7 +150,7 @@ class MyBotHandler(object):
 | 
				
			|||||||
    def usage(self):
 | 
					    def usage(self):
 | 
				
			||||||
        return '''Your description of the bot'''
 | 
					        return '''Your description of the bot'''
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def handle_message(self, message, client, state_handler):
 | 
					    def handle_message(self, message, bot_handler, state_handler):
 | 
				
			||||||
        # add your code here
 | 
					        # add your code here
 | 
				
			||||||
 | 
					
 | 
				
			||||||
handler_class = MyBotHandler
 | 
					handler_class = MyBotHandler
 | 
				
			||||||
@@ -163,7 +163,7 @@ handler_class = MyBotHandler
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
* Every bot needs to implement the functions
 | 
					* Every bot needs to implement the functions
 | 
				
			||||||
    * `usage(self)`
 | 
					    * `usage(self)`
 | 
				
			||||||
    * `handle_message(self, message, client)`
 | 
					    * `handle_message(self, message, bot_handler)`
 | 
				
			||||||
 | 
					
 | 
				
			||||||
* These functions are documented in the [next section](#bot-api).
 | 
					* These functions are documented in the [next section](#bot-api).
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -212,7 +212,7 @@ def usage(self):
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
### handle_message
 | 
					### handle_message
 | 
				
			||||||
 | 
					
 | 
				
			||||||
*handle_message(self, message, client)*
 | 
					*handle_message(self, message, bot_handler)*
 | 
				
			||||||
 | 
					
 | 
				
			||||||
handles user message.
 | 
					handles user message.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -222,7 +222,7 @@ handles user message.
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
* message - a dictionary describing a Zulip message
 | 
					* message - a dictionary describing a Zulip message
 | 
				
			||||||
 | 
					
 | 
				
			||||||
* client - used to interact with the server, e.g. to send a message
 | 
					* bot_handler - used to interact with the server, e.g. to send a message
 | 
				
			||||||
 | 
					
 | 
				
			||||||
* state_handler - used to save states/information of the bot **beta**
 | 
					* state_handler - used to save states/information of the bot **beta**
 | 
				
			||||||
    * use `state_handler.set_state(state)` to set a state (any object)
 | 
					    * use `state_handler.set_state(state)` to set a state (any object)
 | 
				
			||||||
@@ -235,22 +235,22 @@ None.
 | 
				
			|||||||
#### Example implementation
 | 
					#### Example implementation
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 ```
 | 
					 ```
 | 
				
			||||||
  def handle_message(self, message, client, state_handler):
 | 
					  def handle_message(self, message, bot_handler, state_handler):
 | 
				
			||||||
     original_content = message['content']
 | 
					     original_content = message['content']
 | 
				
			||||||
     original_sender = message['sender_email']
 | 
					     original_sender = message['sender_email']
 | 
				
			||||||
     new_content = original_content.replace('@followup',
 | 
					     new_content = original_content.replace('@followup',
 | 
				
			||||||
                                            'from %s:' % (original_sender,))
 | 
					                                            'from %s:' % (original_sender,))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
     client.send_message(dict(
 | 
					     bot_handler.send_message(dict(
 | 
				
			||||||
         type='stream',
 | 
					         type='stream',
 | 
				
			||||||
         to='followup',
 | 
					         to='followup',
 | 
				
			||||||
         subject=message['sender_email'],
 | 
					         subject=message['sender_email'],
 | 
				
			||||||
         content=new_content,
 | 
					         content=new_content,
 | 
				
			||||||
     ))
 | 
					     ))
 | 
				
			||||||
 ```
 | 
					 ```
 | 
				
			||||||
### client.send_message
 | 
					### bot_handler.send_message
 | 
				
			||||||
 | 
					
 | 
				
			||||||
*client.send_message(message)*
 | 
					*bot_handler.send_message(message)*
 | 
				
			||||||
 | 
					
 | 
				
			||||||
will send a message as the bot user.  Generally, this is less
 | 
					will send a message as the bot user.  Generally, this is less
 | 
				
			||||||
convenient than *send_reply*, but it offers additional flexibility
 | 
					convenient than *send_reply*, but it offers additional flexibility
 | 
				
			||||||
@@ -263,7 +263,7 @@ about where the message is sent to.
 | 
				
			|||||||
### Example implementation
 | 
					### Example implementation
 | 
				
			||||||
 | 
					
 | 
				
			||||||
```
 | 
					```
 | 
				
			||||||
client.send_message(dict(
 | 
					bot_handler.send_message(dict(
 | 
				
			||||||
    type='stream', # can be 'stream' or 'private'
 | 
					    type='stream', # can be 'stream' or 'private'
 | 
				
			||||||
    to=stream_name, # either the stream name or user's email
 | 
					    to=stream_name, # either the stream name or user's email
 | 
				
			||||||
    subject=subject, # message subject
 | 
					    subject=subject, # message subject
 | 
				
			||||||
@@ -271,9 +271,9 @@ client.send_message(dict(
 | 
				
			|||||||
))
 | 
					))
 | 
				
			||||||
```
 | 
					```
 | 
				
			||||||
 | 
					
 | 
				
			||||||
### client.send_reply
 | 
					### bot_handler.send_reply
 | 
				
			||||||
 | 
					
 | 
				
			||||||
*client.send_reply(message, response)*
 | 
					*bot_handler.send_reply(message, response)*
 | 
				
			||||||
 | 
					
 | 
				
			||||||
will reply to the triggering message to the same place the original
 | 
					will reply to the triggering message to the same place the original
 | 
				
			||||||
message was sent to, with the content of the reply being *response*.
 | 
					message was sent to, with the content of the reply being *response*.
 | 
				
			||||||
@@ -284,9 +284,9 @@ message was sent to, with the content of the reply being *response*.
 | 
				
			|||||||
 (provided by `handle_message`).
 | 
					 (provided by `handle_message`).
 | 
				
			||||||
* response - Response message from the bot (string).
 | 
					* response - Response message from the bot (string).
 | 
				
			||||||
 | 
					
 | 
				
			||||||
### client.update_message
 | 
					### bot_handler.update_message
 | 
				
			||||||
 | 
					
 | 
				
			||||||
*client.update_message(message)*
 | 
					*bot_handler.update_message(message)*
 | 
				
			||||||
 | 
					
 | 
				
			||||||
will edit the content of a previously sent message.
 | 
					will edit the content of a previously sent message.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -299,7 +299,7 @@ will edit the content of a previously sent message.
 | 
				
			|||||||
From `/zulip/api/bots/incrementor/incrementor.py`:
 | 
					From `/zulip/api/bots/incrementor/incrementor.py`:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
```
 | 
					```
 | 
				
			||||||
client.update_message(dict(
 | 
					bot_handler.update_message(dict(
 | 
				
			||||||
    message_id=self.message_id, # id of message to be updated
 | 
					    message_id=self.message_id, # id of message to be updated
 | 
				
			||||||
    content=str(self.number), # string with which to update message with
 | 
					    content=str(self.number), # string with which to update message with
 | 
				
			||||||
))
 | 
					))
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user