Ejemplo n.º 1
0
async def bot_start(message: Message):
    await message.answer(_('Привіт') + f', {User.get_current().first_name}!',
                         reply_markup=main_menu)
    db.insert_or_ignore(
        'users', {
            'id': User.get_current().id,
            'first_name': User.get_current().first_name,
            'last_name': User.get_current().last_name,
            'lang': User.get_current().locale.language
        })
    await message.answer(_('Виберіть мову'), reply_markup=choose_language)
    await message.answer(_('Натисніть /help, щоб отримати список команд'))
Ejemplo n.º 2
0
async def change_language(call: CallbackQuery):
    await call.message.edit_reply_markup()
    lang = call.data[-2:]
    db.update('users', User.get_current().id, {
        'lang': lang
    })
    # await call.message.edit_text(_("Мова була змінена", locale=lang))
    full_lang = {
        'uk': 'українська',
        'ru': 'русский',
        'en': 'English'
    }
    await call.message.answer(_("Мова була змінена", locale=lang), reply_markup=ReplyKeyboardRemove())
    await call.message.answer(_('Ваша поточна мова', locale=lang) + f': {full_lang[lang]}',
                              reply_markup=get_main_menu(db.get_lang(User.get_current().id)))
Ejemplo n.º 3
0
 def __init__(self) -> None:
     headers = {
         "Authorization": f"Token {settings.API_TOKEN}",
         "Accept-Language": User.get_current().locale.language,
     }
     self._session = aiohttp.ClientSession(headers=headers)
     self._api_base = furl(settings.API_BASE_URL)
Ejemplo n.º 4
0
async def table_handler(call: CallbackQuery):
    await call.message.edit_reply_markup()
    task = call.data.split('_')[-1]
    lang = db.get_lang(User.get_current().id)
    with open(rf'C:\mpr\football-bot\data\tables\{task}_{lang}.jpg',
              'rb') as table_photo:
        await call.message.answer_photo(table_photo)
Ejemplo n.º 5
0
async def add_bug_location(msg: types.Message, state: FSMContext):
    data = await state.get_data()
    await data.get('message').delete_reply_markup()
    default_status = await BugStatus.select('id').where(
        BugStatus.status == 'pending').gino.scalar()

    photo = data.get('photo')

    bug = await Bug.create(photo_path=photo.file_id,
                           description=data.get('description'),
                           location=msg.text,
                           status=default_status,
                           user=TgUser.get_current())

    photo_path = os.path.join(UPLOAD_DIR, f'bugs/{bug.id}.jpg')
    await photo.download(photo_path)
    await bug.update(photo_path=f'bugs/{bug.id}.jpg').apply()

    await bot.send_photo(ADMIN_CHAT_ID,
                         photo.file_id,
                         caption=f'Баг №{bug.id}\n'
                         f'Місцезнаходження: <i>{msg.text}</i>\n'
                         f'Опис: "<i>{data.get("description")}</i>"',
                         reply_markup=get_admin_decision_kb(bug.id))

    await state.finish()
    await msg.answer(
        f'Баг № {bug.id} було надіслано адмінам. '
        f'Найближчим часом інформацію перевірять, та сповістять вас\n\n'
        f'Дякуємо за повідомлення 😊')
Ejemplo n.º 6
0
async def prepare_words_data(set_id: int, photo_ids: list,
                             crop_range: list) -> list:
    words_data = []
    user_id = User.get_current().id

    for photo_id in photo_ids:
        img_file = BytesIO()
        await (await bot.get_file(photo_id)).download(destination=img_file)
        word_img, transl_img = await get_separated_imgs(img_file, *crop_range)
        logging.info(f" --- {word_img} --- {transl_img} ---")

        # If some img from the range smaller than the crop size
        if not word_img:
            return None

        word_img_msg = await bot.send_photo(user_id, word_img)
        word_img_id = word_img_msg.photo[-1].file_id
        await word_img_msg.delete()

        transl_img_msg = await bot.send_photo(user_id, transl_img)
        transl_img_id = transl_img_msg.photo[-1].file_id
        await transl_img_msg.delete()

        words_data.append(
            dict(set_id=set_id,
                 base_img_id=photo_id,
                 word_img_id=word_img_id,
                 transl_img_id=transl_img_id))

    return words_data
Ejemplo n.º 7
0
    async def _send(self, image: BytesIO, *kb_list: InlineKeyboardMarkup):
        bot = Bot.get_current()
        user = User.get_current()
        call = CallbackQuery.get_current()
        message = Message.get_current()
        kb_list = [kb for kb in kb_list if kb]  # clean from None

        if call:
            reply_to = call.message.message_id
        elif message:
            reply_to = message and message.message_id
        else:
            reply_to = None

        if len(kb_list) == 0:
            kb = None
        elif len(kb_list) == 1:
            kb = kb_list[0]
        else:
            kb = inline.merge_inline_keyboards(kb_list[0], kb_list[1])

        await bot.send_photo(user.id,
                             image,
                             reply_markup=kb,
                             reply_to_message_id=reply_to)
Ejemplo n.º 8
0
async def redirect_to_request_language():
    user_id = User.get_current().id
    kb = menus.get_lang_menu()
    resp = messages.request_language.value
    if resp == 'request language':
        resp = 'Select your language:'
    await bot.send_message(user_id, resp, reply_markup=kb)
Ejemplo n.º 9
0
 async def check(self, obj: Union[Message, CallbackQuery]):
     if getattr(obj, "reply_to_message"):
         logger.info(obj.reply_to_message.from_user.id)
         if Chat.get_current().id == User.get_current().id:
             return True
     
     return False
async def get_user(user_id: int) -> User:
    user = await db_engine.find(User, User.id == user_id)
    if not user:
        tg_user = TelegramUser.get_current()
        await check_user(tg_user.id, tg_user.first_name, tg_user.last_name,
                         tg_user.username, tg_user.language_code)
        return await get_user(user_id)
    return user[0]
Ejemplo n.º 11
0
    async def add_new_user(self):
        full_info = User.get_current()

        user_info = f"id: {full_info.id}, username: {full_info.username}, first_name: {full_info.first_name}, last_name: {full_info.last_name}"

        command = self.ADD_NEW_USER
        id_user = await self.pool.fetchval(command, user_info)
        return id_user
Ejemplo n.º 12
0
async def on_err_unknown_exception(_, e: Exception):
    if isinstance(e, BotError):
        return True

    await bot.send_message(User.get_current().id, texts.unknown_exception)
    logger.exception(e)
    sentry_sdk.capture_exception(e)
    return True
    def get_keyboard(self, chat_id: Optional[int] = None):
        if chat_id is None:
            chat_id = User.get_current().id

        if not self.is_inited(chat_id):
            raise NotInitedException('inline_calendar is not inited properly')

        user_info = self._get_user_info(chat_id)

        kb = types.InlineKeyboardMarkup()
        kb.row(*self._create_header(
            chat_id))  # create header with month name and year aka "Aug 2019"
        kb.row(*self._create_weekdays_buttons(
            chat_id))  # create buttons with week days

        f_row = []
        mrange = monthrange(user_info.current_date.year,
                            user_info.current_date.month)
        for i in range(mrange[0]):  # adding the days which were passed
            f_row.append(
                types.InlineKeyboardButton(
                    text=' ',
                    callback_data=InlineCalendar.CALLBACK_WRONG_CHOICE))

        rows = [f_row]
        for i in range(1, mrange[1] + 1):  # adding active days
            curr_date = datetime.date(day=i,
                                      month=user_info.current_date.month,
                                      year=user_info.current_date.year)
            if curr_date.weekday() == 0:
                rows.append([])
            if curr_date < user_info.min_date:
                rows[-1].append(
                    types.InlineKeyboardButton(
                        text=' ',
                        callback_data=InlineCalendar.CALLBACK_WRONG_CHOICE))
            else:
                rows[-1].append(
                    types.InlineKeyboardButton(
                        text=str(i),
                        callback_data=InlineCalendar.BASE_CALLBACK.new(
                            action=Actions.PICK_DAY.name, data=str(i))))

        curr_date = datetime.date(day=mrange[1],
                                  month=user_info.current_date.month,
                                  year=user_info.current_date.year)

        for i in range(curr_date.weekday() + 1, 7):  # adding inactive days
            rows[-1].append(
                types.InlineKeyboardButton(
                    text=' ',
                    callback_data=InlineCalendar.CALLBACK_WRONG_CHOICE))

        for r in rows:
            kb.row(*r)

        kb.row(*self._create_bottom(chat_id))  # adding buttons for pagination
        return kb
Ejemplo n.º 14
0
async def bot_start(msg: types.Message):
    user = User.get_current()

    await db.add_user(id=user.id,
                      username=user.username,
                      full_name=user.full_name)
    await redis_commands.set_new_user(user_id=user.id)

    scheduler.add_job(greeting, args=(msg, ))
Ejemplo n.º 15
0
    async def get_user_locale(self, action, args) -> str:
        user = User.get_current()
        db = dp.get_current()['db']

        try:  # пробуем достать локаль из базы
            return await db.get_user_locale(user.id)
        except LocaleNotFound:  # возвращаем локаль, которую вернул телеграм
            if user.locale:
                return user.locale.language
            return 'ru'
Ejemplo n.º 16
0
async def get_or_set_omer_flag(
    omer_flag: Optional[bool] = None,
    zmanim: Optional[
        Any] = None  # todo circullar import problem, refactor needed
) -> Optional[bool]:
    user = User.get_current()
    if omer_flag:
        omer_time = zmanim and zmanim.tzeis_8_5_degrees.isoformat()
        return await set_omer_flag(user, omer_flag, omer_time)
    return await get_omer_flag(user)
Ejemplo n.º 17
0
        async def wrapper(*args, **kwargs):
            spec = inspect.getfullargspec(func)
            kwargs = {k: v for k, v in kwargs.items() if k in spec.args}

            user = User.get_current()
            msg = Message.get_current()
            attach_message_text and params.update({'text': msg.text})

            posthog.capture(user.id, action, params, message_id=msg.message_id)

            return await func(*args, **kwargs)
Ejemplo n.º 18
0
async def assists_handler(call: CallbackQuery):
    await call.message.edit_reply_markup()
    task = call.data.split('_')[-1]
    lang = db.get_lang(User.get_current().id)
    if task == 'upl' and lang == 'en':
        await call.message.answer(
            'Assistants for this league has not been given')
    else:
        with open(rf'C:\mpr\football-bot\data\assists\{task}_{lang}.jpg',
                  'rb') as table_photo:
            await call.message.answer_photo(table_photo)
Ejemplo n.º 19
0
async def main_errors_handler(_, e: Exception):
    if isinstance(e, ZmanimBotBaseException):
        return True
    user = User.get_current()
    bot = Bot.get_current()
    await bot.send_message(user.id, error_occured)

    logger.exception(e)

    sentry_sdk.capture_exception(e)
    return True
Ejemplo n.º 20
0
    async def _handler(*args) -> List[dict]:
        args = copy.deepcopy(args)

        if User.get_current().locale is None:
            return list(args)

        result = [
            {key: localed(var) if isinstance(var, dict) else var for key, var in task.items()}
            for task in args
        ]

        return result
Ejemplo n.º 21
0
async def change_language(call: CallbackQuery):
    await call.message.edit_text(_('Виберіть лігу'))
    task = call.data[-1]
    if task == 't':
        await call.message.edit_reply_markup(table_choose_liga)
    elif task == 's':
        await call.message.edit_reply_markup(scorers_choose_liga)
    elif task == 'a':
        if db.get_lang(User.get_current().id) != 'en':
            await call.message.edit_reply_markup(assists_choose_liga)
        else:
            await call.message.edit_reply_markup(assists_choose_liga_en)
    elif task == 'm':
        await call.message.edit_reply_markup(matches_choose_liga)
Ejemplo n.º 22
0
async def init_donate(amount: int):
    user = User.get_current()
    invoice_title = messages.donate_invoice_title.value.format(amount)
    price = int(amount) * 100

    await bot.send_invoice(
        user.id,
        title=invoice_title,
        description=messages.donate_invoice_description.value.format(amount),
        payload=f'donate:{amount}:usd:{user.id}:{dt.now().isoformat()}',
        provider_token=config.PAYMENTS_PROVIDER_TOKEN,
        currency='usd',
        prices=[LabeledPrice(invoice_title, price)],
        provider_data=['qweqweqwe'])
Ejemplo n.º 23
0
async def welcome_message(message: Message):
    # Запишем пользователя в базу
    id_user = await db.add_new_user()

    # Выполним функцию, которая отправит пользователю кнопки с доступными категориями
    if str(User.get_current().id) in ADMINS_ID_LIST:
        await message.answer(
            "Привет! Я бот-ассистент. \nВсе возможные действия есть в меню управления.",
            reply_markup=main_admin_keyboard)

    else:
        await message.answer(
            "Привет! Я бот-ассистент. \nС помощью меня ты сможешь посмотреть статистику по играм в колонизаторы.",
            reply_markup=main_keyboard)
Ejemplo n.º 24
0
async def greeting(msg: types.Message):
    user = User.get_current()

    await msg.answer(f"Привет, {user.full_name}!")
    await msg.answer(
        f"Я черешенка, которая будет помогать тебе изучать новые английские слова."
    )
    await msg.answer_sticker(
        "CAACAgIAAxkBAAMDYEy1-FTmlee0a0sLpyxSk-M1LuwAAg0AA8A2TxOk-eH01HiNUx4E")

    await asyncio.sleep(4)

    wordbit_link = "https://play.google.com/store/apps/details?id=net.wordbit.enru"
    await msg.answer(
        f"Изночально весь функционал этого бота будет привязан к приложению <a href='{wordbit_link}'>WordBit</a> "
        f"или к любому другому, в котором слово и перевод будут находиться на одном экране.\n\n"
        f"Качай WordBit —> делай скрины слов, которые не знаешь —> заливай пачки скриншотов в бота "
        f"—> учи загруженные наборы слов 😉",
        disable_web_page_preview=True)
    await msg.answer_sticker(
        "CAACAgIAAxkBAAMFYEy83a4vWMnl1iaN1Gi07fd6fugAAh0AA8A2TxNe2KbcQT3eSB4E")

    await asyncio.sleep(14)

    await msg.answer(
        "Преимущество этого бота в том, что он будет:\n"
        "1) предоставлять удобный механизм изучения слов;\n"
        "2) напоминать, когда нужно повторить определённый набор, для того чтобы "
        "ты не забывал выученные слова;\n"
        "3) позволять дописывать свои ассоциации к словам;\n"
        "4) давать возможность делиться наборами c другими участниками бота и "
        "весело придумывать ассоциации к словам вместе с друзьями.")
    await msg.answer_sticker(
        "CAACAgIAAxkBAAMHYEy-1ZR0YqSU-36ANwmSftFGAmkAAgYAA8A2TxPHyqL0sm5wdh4E")

    await asyncio.sleep(14)

    await msg.answer(
        "И помни, что чем глупее и постыднее ассоциация к слову — тем лучше она запомниться!",
        reply_markup=keyboards.default.get_bot_menu())
    await msg.answer_sticker(
        "CAACAgIAAxkBAAIJeWBWMPFTB6J-aY0eU2oWK_NVJ5B2AAIIAAPANk8Tb2wmC94am2keBA"
    )

    logging.info(f"@{user.username}-{user.id} start bot")
Ejemplo n.º 25
0
async def show_sets(msg: Message, state: FSMContext):
    user = User.get_current()

    await clean_previous_menu_msg(msg, state)
    await state.finish()

    await state.update_data(page=1)

    sets_msg = await msg.answer(
        "Доступные наборы слов:",
        reply_markup=await keyboards.inline.get_sets_menu(user.id)
    )

    await state.update_data(sets_msg_id=sets_msg.message_id)

    await change_bot_menu(state)

    logging.info(f"Show connection menu for @{user.username}-{user.id}")
Ejemplo n.º 26
0
    async def _update(self, image: BytesIO, *kb_list: InlineKeyboardMarkup):
        bot = Bot.get_current()
        user = User.get_current()
        call = CallbackQuery.get_current()

        media = InputMedia(media=image)

        if len(kb_list) == 0:
            kb = None
        elif len(kb_list) == 1:
            kb = kb_list[0]
        else:
            kb = inline.merge_inline_keyboards(kb_list[0], kb_list[1])

        await bot.edit_message_media(media,
                                     user.id,
                                     call.message.message_id,
                                     reply_markup=kb)
Ejemplo n.º 27
0
async def command_handler(message: Message):
    task = message.text[1]
    if task == 't':
        await message.answer(_('Виберіть лігу'),
                             reply_markup=table_choose_liga)
    elif task == 's':
        await message.answer(_('Виберіть лігу'),
                             reply_markup=scorers_choose_liga)
    elif task == 'a':
        if db.get_lang(User.get_current().id) != 'en':
            await message.answer(_('Виберіть лігу'),
                                 reply_markup=assists_choose_liga)
        else:
            await message.answer(_('Виберіть лігу'),
                                 reply_markup=assists_choose_liga_en)
    elif task == 'm':
        await message.answer(_('Виберіть лігу'),
                             reply_markup=matches_choose_liga)
Ejemplo n.º 28
0
async def change_bot_menu(state: FSMContext):
    user = User.get_current()

    phrase = await db.get_random_phrase()
    set_id = (await state.get_data()).get("set_id", None)

    msg: Message
    if set_id:
        msg = await bot.send_message(
            user.id,
            text=phrase,
            reply_markup=keyboards.default.get_bot_menu(in_set=True))
    else:
        msg = await bot.send_message(
            user.id,
            text=phrase,
            reply_markup=keyboards.default.get_bot_menu(in_set=False))

    await state.update_data(last_phrase_msg_id=msg.message_id)

    logging.info(f"Update menu for @{user.username}-{user.id}")
    def init(self,
             base_date: datetime.date,
             min_date: datetime.date,
             max_date: datetime.date,
             chat_id: Optional[int] = None,
             month_names: List[str] = None,
             days_names: List[str] = None):
        """
        Default language is English
        :param chat_id: chat id
        :param base_date: a datetime.date object.
        :param min_date: a datetime.date object.
        :param max_date: a datetime.date object.
        :param month_names: 12-element list for month names. If none, then English names will be used
        :param days_names: 7-element list fo2r month names. If none, then English names will be used
        """
        if chat_id is None:
            chat_id = User.get_current().id

        if not (min_date <= base_date <= max_date):
            raise ValueError(
                "Base_date is less than min_date or more than max_date")

        if month_names is None:
            month_names = [
                'Jan', 'Feb', 'Mar', 'Apr', 'May', 'June', 'July', 'Aug',
                'Sept', 'Oct', 'Nov', 'Dec'
            ]
        if len(month_names) != 12:
            raise ValueError('Length of month_names != 12')

        if days_names is None:
            days_names = ['Mon', 'Tu', 'Wed', 'Th', 'Fr', 'Sat', 'Sun']
        if len(days_names) != 7:
            raise ValueError('Length of days_names != 7')

        self._set_user_info(
            chat_id,
            InlineCalendarData(chat_id, min_date, max_date, base_date,
                               month_names, days_names))
Ejemplo n.º 30
0
async def get_or_set_processor_type(
        processor_type: Optional[str] = None) -> Optional[str]:
    user = User.get_current()
    return await get_processor_type(user) if not processor_type \
        else await set_processor_type(user, processor_type)