from __future__ import print_function
import sys
import os

import json
import logging
import re

from firebase import firebase

logger = logging.getLogger(__name__)


def eprint(*args, **kwargs):
    print(*args, file=sys.stderr, **kwargs)

class RtmEventHandler(object):
    def __init__(self, slack_clients, msg_writer):
        self.clients = slack_clients
        self.msg_writer = msg_writer
        self.firebase = firebase.FirebaseApplication(os.getenv("FIREBASE_URL"), None)
        self.firebase.authentication = firebase.FirebaseAuthentication(os.getenv("FIREBASE_SECRET"), os.getenv("FIREBASE_EMAIL"))

    def handle(self, event):

        if 'type' in event:
            self._handle_by_type(event['type'], event)

    def _handle_by_type(self, event_type, event):
        # See https://api.slack.com/rtm for a full list of events
        if event_type == 'error':
            # error
            self.msg_writer.write_error(event['channel'], json.dumps(event))
        elif event_type == 'message':
            # message was sent to channel
            self._handle_message(event)
        elif event_type == 'channel_joined':
            # you joined a channel
            self.msg_writer.write_help_message(event['channel'])
        elif event_type == 'group_joined':
            # you joined a private group
            self.msg_writer.write_help_message(event['channel'])
        else:
            pass

    def _save(self, key, value):
        if self.firebase.get('/glossary', key):
            return False
        self.firebase.post('/glossary/' + key.lower(), value)
        return True

    def _get(self, key):
        result = self.firebase.get('/glossary', key.lower())
        if result:
            return result.itervalues().next()
        return False

    def _clean_links(self, message):
        return re.sub("<([^@\#\!].*?)(\|.*?)?>", r'\1', message)

    def _is_hidden_message_event(self, event):
        return 'hidden' in event and event['hidden'] == True

    def _handle_message(self, event):
        # Filter out messages from the bot itself
        if not self._is_hidden_message_event(event) and not self.clients.is_message_from_me(event['user']):

            msg_txt = event['text']

            if self.clients.is_bot_mention(msg_txt):

                add_definition_regexp = re.search("^<@{}>[\s:]+(.+)\s*=\s*(.+)".format(re.escape(self.clients.bot_user_id())), msg_txt, re.MULTILINE|re.DOTALL)
                get_definition_regexp = re.search("^<@{}>[\s:]+(.+)".format(re.escape(self.clients.bot_user_id())), msg_txt, re.MULTILINE|re.DOTALL)
                
                self.clients.send_user_typing(event['channel'])
                
                if add_definition_regexp:
                    key = add_definition_regexp.group(1)
                    value = add_definition_regexp.group(2)
                    value = self._clean_links(value)
                    if self._save(key, value):
                        self.msg_writer.send_message(event['channel'], u'Saved `{}` as: ```{}```'.format(key, value))
                    else:
                        self.msg_writer.send_message(event['channel'], u'`{}` already defined as: ```{}```'.format(key, self._get(key)))
                elif get_definition_regexp:
                    key = get_definition_regexp.group(1)
                    value = self._get(key)
                    if value:
                        self.msg_writer.send_message(event['channel'], u'Definition for `{}` is: ```{}```'.format(key, value))
                    else:
                        self.msg_writer.send_message(event['channel'], u'`{}` is not defined yet. Use: `<@{}> {}=Definition text` to define'.format(key, self.clients.bot_user_id(), key))                
                else:
                    self.msg_writer.send_message(event['channel'], u'Wrong input')

@@ -89,16 +89,30 @@
 mport re
+%0Aimport urllib
 %0A%0Afrom f
@@ -1482,34 +1482,13 @@
 elf.
-firebase.get('/glossary', 
+_get(
 key)
@@ -1556,26 +1556,61 @@
 ary/' + 
-key.lower(
+urllib.quote_plus(key.lower().encode('utf-8')
 ), value
@@ -1705,26 +1705,61 @@
 ssary', 
-key.lower(
+urllib.quote_plus(key.lower().encode('utf-8')
 ))%0A     

Update event_handler.py