Exemplo n.º 1
0
def on_profile(message: types.Message):
    u_id = message.from_user.id
    api_url = environ.get('API_URL')

    res = graphql_request(api_url,
                          telegramToUserId.format(u_id),
                          telegram_id=u_id)

    if res.get('errors', None):
        bot.reply_to(message, localization['register_first'])
        return

    internal_id = res['data']['telegramToUserId']

    res = graphql_request(api_url,
                          profile.format(internal_id),
                          telegram_id=u_id)['data']['user']

    user_data = {
        'name': res['name'],
        'money': res['money'],
        'transactions': len(res['transactions']),
    }

    text_response = localization['profile'].format(**user_data)

    if res['is_admin']:
        text_response += localization['profile_admin'].format(res['is_admin'])

    bot.send_message(u_id, text_response)
def get_transactions(user_id=None, telegram_id=None):
    api_url = environ.get('API_URL')

    if not user_id:
        res = graphql_request(api_url,
                              telegramToUserId.format(telegram_id),
                              telegram_id=telegram_id)

        if res.get('errors', None):
            return None

        user_id = res['data']['telegramToUserId']

    res = graphql_request(api_url, transactions.format(user_id),
                          u_id=user_id)['data']['user']

    if res.get('errors', None):
        return None

    return res
Exemplo n.º 3
0
def empty_query(query: types.InlineQuery):
    u_id = query.from_user.id
    api_url = environ.get('API_URL')

    res = graphql_request(api_url,
                          telegramToUserId.format(u_id),
                          telegram_id=u_id)

    if res.get('errors', None):
        on_inline_not_registered(query)
        return

    internal_id = res['data']['telegramToUserId']
    res = graphql_request(api_url,
                          profile.format(internal_id),
                          telegram_id=u_id)['data']['user']
    money = res['money']

    balance = types.InlineQueryResultArticle(
        id='1',
        title=localization['inline_mode']['balance']['title'].format(money),
        description=localization['inline_mode']['balance']['description'],
        input_message_content=types.InputTextMessageContent(
            message_text=localization['inline_mode']['balance']
            ['message_text'].format(money)),
        thumb_url=localization['inline_mode']['balance']['thumb_url'])

    instructions = types.InlineQueryResultArticle(
        id='2',
        title=localization['inline_mode']['empty']['title'],
        description=localization['inline_mode']['empty']['description'],
        input_message_content=types.InputTextMessageContent(
            message_text=localization['inline_mode']['empty']['message_text']),
        thumb_url=localization['inline_mode']['empty']['thumb_url'])

    bot.answer_inline_query(query.id, [balance, instructions],
                            cache_time=environ.get('INLINE_QUERY_CACHE_TIME'))
Exemplo n.º 4
0
def answer_query(query: types.InlineQuery):
    u_id = query.from_user.id
    api_url = environ.get('API_URL')

    res = graphql_request(api_url,
                          telegramToUserId.format(u_id),
                          telegram_id=u_id)

    if res.get('errors', None):
        on_inline_not_registered(query)
        return

    internal_id = res['data']['telegramToUserId']
    res = graphql_request(api_url,
                          profile.format(internal_id),
                          telegram_id=u_id)['data']['user']
    money = res['money']

    try:
        matches = re.match(r'(\d+)? ?(.*)', query.query)
        num = matches.groups()[0]
        message = matches.groups()[1]
    except AttributeError:
        return

    if not num or int(num) <= 0:
        empty_query(query)
        return

    num = int(num)
    test = len('xxxx;{};{};{}'.format(u_id, num, message).encode('utf-8'))

    if test > 64:
        on_callback_data_overflow(query)
        return

    if num >= (2**32 - 1):
        on_integer_overflow(query)
        return

    if num > money:
        give = types.InlineQueryResultArticle(
            id='2',
            title=localization['inline_mode']['not_enough']['title'],
            description=localization['inline_mode']['not_enough']
            ['description'],
            input_message_content=types.InputTextMessageContent(
                message_text=localization['inline_mode']['not_enough']
                ['message_text']),
            thumb_url=localization['inline_mode']['not_enough']['thumb_url'])

    else:
        give_kb = types.InlineKeyboardMarkup()
        give_kb.row(
            types.InlineKeyboardButton(
                localization['inline_keyboard']['receive'],
                callback_data='give;{};{};{}'.format(u_id, num, message)),
            types.InlineKeyboardButton(
                localization['inline_keyboard']['cancel'],
                callback_data='cancel_request;{}'.format(u_id)))

        give = types.InlineQueryResultArticle(
            id='2',
            title=localization['inline_mode']['give']['title'].format(num),
            description=(localization['inline_mode']['give']
                         ['description'].format(message) if message else
                         localization['inline_mode']['no_message']),
            input_message_content=types.InputTextMessageContent(
                message_text=localization['inline_mode']['give']
                ['message_text'].format(num) +
                (localization['inline_mode']['message_text_trans_message'].
                 format(message) if message else ''),
                parse_mode='HTML'),
            reply_markup=give_kb,
            thumb_url=localization['inline_mode']['give']['thumb_url'])

    ask_kb = types.InlineKeyboardMarkup()
    ask_kb.row(
        types.InlineKeyboardButton(localization['inline_keyboard']['give'],
                                   callback_data='recv;{};{};{}'.format(
                                       u_id, num, message)),
        types.InlineKeyboardButton(
            localization['inline_keyboard']['cancel'],
            callback_data='cancel_request;{}'.format(u_id)))

    ask = types.InlineQueryResultArticle(
        id='3',
        title=localization['inline_mode']['request']['title'].format(num),
        description=(localization['inline_mode']['request']
                     ['description'].format(message) if message else
                     localization['inline_mode']['no_message']),
        input_message_content=types.InputTextMessageContent(
            message_text=localization['inline_mode']['request']
            ['message_text'].format(num) +
            (localization['inline_mode']['message_text_trans_message'].format(
                message) if message else ''),
            parse_mode='HTML'),
        reply_markup=ask_kb,
        thumb_url=localization['inline_mode']['request']['thumb_url'])

    balance = types.InlineQueryResultArticle(
        id='1',
        title=localization['inline_mode']['balance']['title'].format(money),
        description=localization['inline_mode']['balance']['description'],
        input_message_content=types.InputTextMessageContent(
            message_text=localization['inline_mode']['balance']
            ['message_text'].format(money)),
        thumb_url=localization['inline_mode']['balance']['thumb_url'])

    bot.answer_inline_query(query.id, [balance, give, ask],
                            cache_time=environ.get('INLINE_QUERY_CACHE_TIME'))
Exemplo n.º 5
0
def on_callback_query(query: types.CallbackQuery):
    u_id = query.from_user.id
    user_str = get_user_str(query.from_user)

    title = query.data.split(';')[0]
    value = query.data.split(';')[1:]

    if title == 'register':
        if value[0] == '1':
            user_data = {
                'name': user_str,
                'telegram_id': u_id,
                'username': query.from_user.username
            }

            requests.post(environ.get('API_URL') + '/register',
                          data=user_data).json()

            bot.edit_message_text(localization['register_success'], u_id,
                                  query.message.message_id)

            end_date = datetime.strptime(environ.get('STOP_WHAT_IS_NEW'),
                                         '%Y-%m-%d %H:%M:%S')
            if datetime.now() <= end_date:
                bot.send_message(u_id, localization['what_is_new'])

            time.sleep(1)
            bot.send_message(u_id, localization['try_help'])

        else:
            bot.edit_message_text(localization['register_cancel'], u_id,
                                  query.message.message_id)

    elif title == 'give':
        if str(u_id) == value[0]:
            bot.answer_callback_query(query.id, localization['cannot'])
            return

        from_user_id = graphql_request(
            environ.get('API_URL'),
            telegramToUserId.format(value[0]),
            telegram_id=value[0])['data']['telegramToUserId']

        res = get_transactions(user_id=from_user_id)

        for t in res['transactions']:
            if t['queryId'] == query.inline_message_id:
                return

        to_user_id = graphql_request(environ.get('API_URL'),
                                     telegramToUserId.format(u_id),
                                     telegram_id=u_id)

        if to_user_id.get('errors', None):
            bot.edit_message_text(localization['register_first'],
                                  inline_message_id=query.inline_message_id)
            bot.reply_to()
            return

        to_user_id = to_user_id['data']['telegramToUserId']

        res = graphql_request(environ.get('API_URL'),
                              transfer.format(value[1], from_user_id,
                                              to_user_id, value[2],
                                              query.inline_message_id),
                              telegram_id=value[0])

        if res.get('errors', None):
            bot.answer_callback_query(query.id, res['errors'][0]['message'])
            return

        bot.edit_message_text(localization['transaction_success'].format(
            value[1]),
                              inline_message_id=query.inline_message_id)

        name = '@' + \
            query.from_user.username if query.from_user.username else get_user_str(
                query.from_user)

        bot.send_message(
            value[0], localization['notification_give'].format(value[1], name))

    elif title == 'recv':
        if str(u_id) == value[0]:
            bot.answer_callback_query(query.id, localization['cannot'])
            return

        from_user_id = graphql_request(environ.get('API_URL'),
                                       telegramToUserId.format(u_id),
                                       telegram_id=u_id)

        if from_user_id.get('errors', None):
            bot.edit_message_text(localization['register_first'],
                                  inline_message_id=query.inline_message_id)
            return

        from_user_id = from_user_id['data']['telegramToUserId']
        res = get_transactions(user_id=from_user_id)

        for t in res['transactions']:
            if t['queryId'] == query.inline_message_id:
                return

        to_user_id = graphql_request(
            environ.get('API_URL'),
            telegramToUserId.format(value[0]),
            telegram_id=value[0])['data']['telegramToUserId']

        res = graphql_request(environ.get('API_URL'),
                              transfer.format(value[1], from_user_id,
                                              to_user_id, value[2],
                                              query.inline_message_id),
                              telegram_id=u_id)

        if res.get('errors', None):
            bot.answer_callback_query(query.id, res['errors'][0]['message'])
            return

        bot.edit_message_text(localization['transaction_success'].format(
            value[1]),
                              inline_message_id=query.inline_message_id)

        name = '@' + \
            query.from_user.username if query.from_user.username else get_user_str(
                query.from_user)

        bot.send_message(
            value[0],
            localization['notification_request'].format(name, value[1]))

    elif title == 'cancel_request':
        if str(query.from_user.id) != value[0]:
            bot.answer_callback_query(query.id, localization['cannot'])
            return

        bot.edit_message_text(localization['transaction_cancel'],
                              inline_message_id=query.inline_message_id)
Exemplo n.º 6
0
def user_exists(telegram_id, api_url):
    res = graphql_request(api_url,
                          telegramToUserId.format(telegram_id),
                          telegram_id=telegram_id)

    return False if res.get('errors') else True