def __init__(self, token):
     super(TelegramBot, self).__init__(token)
     self.instagram = InstagramUtils()
class TelegramBot(TelegramBotHelper):
    database = SqliteDatabase("bot.db", threadlocals=True)

    def __init__(self, token):
        super(TelegramBot, self).__init__(token)
        self.instagram = InstagramUtils()

    def process_None(self, update, command, params):
        chat_id = update.message.chat_id
        self.bot.sendChatAction(chat_id=chat_id, action=telegram.ChatAction.TYPING)
        self.bot.sendMessage(chat_id=chat_id, text="I don't understand you")

    def process_NotExists(self, update, command, params):
        chat_id = update.message.chat_id
        self.bot.sendChatAction(chat_id=chat_id, action=telegram.ChatAction.TYPING)
        self.bot.sendMessage(chat_id=chat_id, text="Command '/%s' doesn't exist" % command)

    def process_GetUser(self, update, command, params):
        from_user = update.message.from_user
        user, _ = User.get_or_create(username=from_user.username, chat_id=update.message.chat_id)
        user.first_name = from_user.first_name
        user.last_name = from_user.last_name
        user.save()

    @request_limit(5)
    def process_weather(self, update, command, params):
        chat_id = update.message.chat_id
        params = params or "Minsk"
        self.bot.sendChatAction(chat_id=chat_id, action=telegram.ChatAction.TYPING)
        r = requests.get("http://api.openweathermap.org/data/2.5/weather?q=%s&units=metric" % params).json()
        if r.get("cod", 200) == "404":
            self.bot.sendMessage(chat_id=chat_id, text="City '%s' not found" % params)
        else:
            self.bot.sendMessage(
                chat_id=chat_id,
                text="%s:\r\n%s. \r\nTemp - %s C. \r\nWind - %s m/sec"
                % (r["name"], r["weather"][0]["description"], r["main"]["temp"], r["wind"]["speed"]),
            )

    @moderator_permission
    def process_op(self, update, command, params):
        chat_id = update.message.chat_id
        if params:
            params = params.replace(" ", "").lower()
            moderator = Moderator.get_or_create(username=params)
            self.bot.sendChatAction(chat_id=chat_id, action=telegram.ChatAction.TYPING)
            self.bot.sendMessage(chat_id=chat_id, text="Added permissions for %s" % params)
        else:
            moderators = Moderator.select()
            moderators = map(lambda x: x.username, moderators)
            moderators = "\r\n".join(moderators)
            self.bot.sendChatAction(chat_id=chat_id, action=telegram.ChatAction.TYPING)
            self.bot.sendMessage(chat_id=chat_id, text="Ops:\r\n%s" % (moderators or "-----"))

    @admin_permission
    @params_required
    def process_deop(self, update, command, params):
        chat_id = update.message.chat_id
        params = params.replace(" ", "").lower()
        moderator = Moderator.select().where(Moderator.username == params)
        if moderator.count():
            moderator[0].delete_instance()
        self.bot.sendChatAction(chat_id=chat_id, action=telegram.ChatAction.TYPING)
        self.bot.sendMessage(chat_id=chat_id, text="Removed permissions for %s" % params)

    @moderator_permission
    @params_required
    def process_addgirl(self, update, command, params):
        chat_id = update.message.chat_id
        params = params.replace(" ", "").lower()
        girl, created = Girl.get_or_create(username=params)
        self.bot.sendChatAction(chat_id=chat_id, action=telegram.ChatAction.TYPING)
        if not created:
            self.bot.sendMessage(chat_id=chat_id, text="Girl '%s' already exists" % params)
        else:
            instagram_user = self.instagram.get_user_by_username(params)
            if instagram_user and self.instagram.load_user_feed(instagram_user.id, count=1) is not None:
                girl.user_id = instagram_user.id
                girl.save()
                self.bot.sendMessage(chat_id=chat_id, text="Added girl '%s'" % params)
            else:
                girl.delete_instance()
                self.bot.sendMessage(chat_id=chat_id, text="Cannot add this girl(" % params)

    @moderator_permission
    @params_required
    def process_removegirl(self, update, command, params):
        params = params.replace(" ", "").lower()
        chat_id = update.message.chat_id
        self.bot.sendChatAction(chat_id=chat_id, action=telegram.ChatAction.TYPING)
        girls = list(Girl.select(Girl.username == params))
        if girls:
            girls[0].delete_instance()
        self.bot.sendMessage(chat_id=chat_id, text="Removed girl '%s'" % params)

    @request_limit(5)
    def process_girl(self, update, command, params):
        girls = list(Girl.select())
        chat_id = update.message.chat_id
        if girls:
            self.bot.sendChatAction(chat_id=chat_id, action=telegram.ChatAction.UPLOAD_PHOTO)
            girl = random.choice(girls)
            media = self.instagram.load_user_feed(girl.user_id, count=50)
            media = random.choice(media)
            image_url = media.images["standard_resolution"].url
            self.bot.sendPhoto(chat_id=chat_id, photo=image_url, caption=self.instagram.get_user_page(girl.username))
        else:
            self.bot.sendChatAction(chat_id=chat_id, action=telegram.ChatAction.TYPING)
            self.bot.sendMessage(chat_id=chat_id, text="No girls(")