Beispiel #1
0
    def download_sticker_async(self, file_id, update=None, context=None):
        if not update or not context:
            return -1

        # language
        locale = update.effective_user.language_code

        # parse update and context
        bot = context.bot
        sticker = bot.get_file(file_id)
        sticker_name = sticker.file_path.split('/')[-1].split('.')[0]
        sticker_unique_id = random_string()  # sticker.file_unique_id

        chat = update.effective_chat
        message = update.effective_message

        # send action
        chat.send_action('upload_document')

        # download sticker
        file_dir = os.path.join(_temp_dir, sticker_name)
        file_path = os.path.join(_temp_dir, sticker_name,
                                 sticker_name + '.png')
        if os.path.isfile(file_path):
            pass
        elif os.path.isdir(file_dir):
            # wait 10 times, 10 seconds for each
            for _ in range(10):
                if not os.path.isfile(file_path):
                    time.sleep(10)
                else:
                    break
            else:
                message.reply_text(l10n('zip_timeout', locale))
                return -1
        else:
            # make dir `sticker_name`
            os.path.isdir(file_dir) or os.makedirs(file_dir)
            _, file_path = download_sticker(sticker, save_dir=file_dir)

        # send document
        sticker_set_name = message.sticker.set_name
        callback_data = ''.join([STICKER_SET, ':', sticker_set_name])
        keyboard = InlineKeyboardMarkup([[
            InlineKeyboardButton(text=l10n('kb_sticker_set', locale),
                                 callback_data=callback_data),
        ]])
        with open(file_path, 'rb') as f:
            chat.send_document(f,
                               reply_markup=keyboard,
                               filename=sticker_unique_id + '.png',
                               reply_to_message_id=message.message_id)

        # count usage
        set_usage(context, file_path=file_path)
Beispiel #2
0
    def base_send_zip(self,
                      file_name,
                      callback,
                      callback_args,
                      update=None,
                      context=None,
                      pack_name=None):
        if not update and not context:
            return -1

        # language
        locale = update.effective_user.language_code

        # parse update and context
        chat = update.effective_chat
        message = update.effective_message

        try:
            file_dir = os.path.join(_temp_dir, file_name)
            zip_file_path = os.path.join(_temp_dir, file_name + '.zip')
            if os.path.isfile(zip_file_path):
                pass
            elif os.path.isdir(file_dir):
                message.reply_text(l10n('zip_packing', locale))
                # wait 10 times, 30 seconds for each
                for _ in range(10):
                    if not os.path.isfile(zip_file_path):
                        time.sleep(30)
                    else:
                        break
                else:
                    message.reply_text(l10n('zip_timeout', locale))
                    return -1
            else:
                message.reply_text(l10n('zip_preparing', locale))
                zip_file_path = callback(*callback_args)

            with open(zip_file_path, 'rb') as f:
                filename = pack_name or file_name
                chat.send_action('upload_document')
                chat.send_document(f,
                                   filename=filename + '.zip',
                                   reply_to_message_id=message.message_id)

            # count usage
            set_usage(context, file_path=zip_file_path)

        except Exception as e:
            message.reply_text(l10n('exec_error', locale))
            self.logger.error('Exception msg: %s', str(e), exc_info=True)
Beispiel #3
0
    def download_sticker_animated_async(self,
                                        file_id,
                                        update=None,
                                        context=None):
        if not update or not context:
            return -1

        # language
        locale = update.effective_user.language_code
        message = update.effective_message

        try:
            # parse update and context
            bot = context.bot
            sticker = bot.get_file(file_id)
            sticker_pack_name = sticker.file_path.split('/')[-1].split('.')[0]
            sticker_unique_id = random_string()  # sticker.file_unique_id

            callback = download_sticker_animated_pack
            args = (sticker, sticker_pack_name)
            self.base_send_zip(sticker_pack_name,
                               callback,
                               args,
                               update=update,
                               context=context,
                               pack_name=sticker_unique_id)

        except Exception as e:
            message.reply_text(l10n('exec_error', locale))
            self.logger.error('Exception msg: %s', str(e), exc_info=True)
Beispiel #4
0
 def warpper(*args, **kwargs):
     _, update, context, *_ = args
     if is_usage_exceed(context):
         # language
         locale = update.effective_user.language_code
         message = l10n('limit_exceed', locale) % {
             'limit': LIMITATION_STRING
         }
         update.effective_message.reply_text(message)
         return None
     else:
         return func(*args, **kwargs)
Beispiel #5
0
    def cmd_gif(self, update, context):
        ''''''
        # language
        locale = update.effective_user.language_code

        file_id = update.effective_message.document.file_id
        file_size = update.effective_message.document.file_size or 1.0e10
        if file_size > self.gif_file_size_max:
            update.effective_message.reply_text(
                l10n('file_size_exceed', locale))
        else:
            self.download_gif_pack_async(file_id,
                                         update=update,
                                         context=context)
Beispiel #6
0
    def cmd_sticker(self, update, context):
        ''''''
        # language
        locale = update.effective_user.language_code

        file_id = update.effective_message.sticker.file_id
        if update.effective_message.sticker.is_animated:
            update.effective_message.reply_text(l10n('unsupport', locale))
            # print(update.effective_message.sticker)
            # self.download_sticker_animated_async(file_id, update=update, context=context)
        else:
            self.download_sticker_async(file_id,
                                        update=update,
                                        context=context)
Beispiel #7
0
    def cmd_start(self, update, context):
        ''''''
        # language
        locale = update.effective_user.language_code

        user = update.effective_user
        user_name = '@' + str(user.username)
        first_name = user.first_name
        last_name = user.last_name
        if first_name and last_name:
            user_name = first_name + ' ' + last_name
        elif first_name:
            user_name = first_name
        elif last_name:
            user_name = last_name

        update.message.reply_text(l10n('start', locale) % {'user': user_name})
Beispiel #8
0
 def cmd_help(self, update, context):
     ''''''
     # language
     locale = update.effective_user.language_code
     update.message.reply_markdown(l10n('help', locale))