• decorator is the name used for a software design pattern
  • A Python decorator is a specific change to the Python syntax that allows us to more conveniently alter functions and methods

Example

def say_hi_decorator(func):
  def func_wrapper():
    print( "You walk to a fast food store." ) # BEFORE
    func()
    print( "You say 'Hi'." )                  # AFTER
  return func_wrapper
@say_hi_decorator
def meet_friend():
  print "You meet your friend."

meet_friend() You walk to a fast-food store. You meet your friend. You say ‘Hi’.

@say_hi_decorator
def meet_ironman():
  print "You meet iron man."

meet_ironman() You walk to a fast-food store. You meet iron man. You say ‘Hi’.

Read More

chat.py

We will create decorator methods to generate and encode Telegram bot reply content.

def __init__(self, body):
    self.body = body
    self.message = self.__get_message()

def __get_message(self):
    if self.body:
        if 'message' in self.body:
            message = self.body['message']
        elif 'edited_message' in self.body:
            message = self.body['edited_message']
        return message
    return None

def __encode_reply_decorator(func):
    def func_wrapper(self, **kwargs):
        method, message_dict = func(self, **kwargs)
        return method, urllib.urlencode(message_dict)
    return func_wrapper

def __get_reply_decorator(func):
    def func_wrapper(self):
        method, reply_dict = func(self)
        reply_dict['chat_id'] = str(self.message['chat']['id'])
        reply_dict['reply_to_message_id'] = str(self.message['message_id'])
        return method, reply_dict
    return func_wrapper

@__encode_reply_decorator
@__get_reply_decorator
def __get_default_reply(self):
    msg = 'I understand only text and sticker messages'
    data = {
        'text': msg.encode('utf-8')
    }
    return 'sendMessage', data

@__encode_reply_decorator
@__get_reply_decorator
def __get_text_reply(self):
    first_name = self.message['from']['first_name']
    msg = 'Yo {} I got your message'.format(first_name)
    data = {
        'text': msg.encode('utf-8')
    }
    return 'sendMessage', data

@__encode_reply_decorator
@__get_reply_decorator
def __get_sticker_reply(self):
    file_id = self.message['sticker']['file_id']
    data = {
        'sticker': file_id.encode('utf-8'),
    }
    return 'sendSticker', data

def get_reply(self):
    if self.message:
        if 'sticker' in self.message:
            return self.__get_sticker_reply()
        elif 'text' in self.message:
            return self.__get_text_reply()
        else:
            return self.__get_default_reply()
    return None, None