Example #1
0
def set_token(bot, update, args):
    if len(args) != 1:
        bot.send_message(chat_id=update.message.chat_id,
                         text='Используй /token <ваш TimePad токен>')
        return
    token = args[0]

    data = timepad.introspect(token)
    if data is None:
        bot.send_message(chat_id=update.message.chat_id,
                         text='Не получилось получить данные, попробуй позже')
        return
    active = data.get('active', False)
    if not active:
        bot.send_message(chat_id=update.message.chat_id,
                         text='Некорректный токен')
        logging.info(repr(data))
        return

    connector = database.Connector()
    last_timestamp = 0
    city = 'Москва'  # default city

    connector.set_timepad_data_for_chat_id(update.message.chat_id,
                                           data['user_id'], data['user_email'],
                                           token, city, last_timestamp)
    events = timepad.get_user_events(token)
    user = connector.get_user_by_chat_id(update.message.chat_id)
    connector.add_user_events(user['id'], events)
    bot.send_message(chat_id=update.message.chat_id,
                     text='Успех\n Список всех команд: /help')
Example #2
0
def show_subscriptions_handler(bot, update):
    connector = database.Connector()
    user = connector.get_user_by_chat_id(update.message.chat_id)
    subscriptions = connector.get_subscriptions(user['id'])
    message = '\n'.join(['Подписки:'] + list('@' + subscribed['tg_name']
                                             for subscribed in subscriptions))
    bot.send_message(chat_id=update.message.chat_id, text=message)
Example #3
0
def start(bot, update):
    connector = database.Connector()
    user = connector.get_user_by_chat_id(update.message.chat_id)
    if user is None:
        connector.add_user(update.message.chat_id,
                           update.message.from_user.username)
    bot.send_message(
        chat_id=update.message.chat_id,
        text="Великолепный бот\nСписок всех команд: /help\nПолучить токен: "
        "https://dev.timepad.ru/api/oauth/")
Example #4
0
def set_city(bot, update, args):
    connector = database.Connector()
    user = connector.get_user_by_chat_id(update.message.chat_id)
    if len(args) == 0:
        city = connector.get_city(user['id'])
        bot.send_message(chat_id=update.message.chat_id,
                         text='Ты в городе {}'.format(city))
    else:
        city = str(*args)
        connector.set_city(user['id'], city)
        bot.send_message(chat_id=update.message.chat_id,
                         text='Теперь ты в городе {}'.format(city))
Example #5
0
def crawl_new_events(bot, job):
    connector = database.Connector()
    user = connector.get_user_for_crawl()
    if user is None:
        return
    events = set(timepad.get_user_events(user['token']))
    old_events = set(connector.get_user_events(user['id']))
    new_events = events - old_events
    if len(new_events) > 0:
        logging.info('Notifying subscribers of {}'.format(str(user['id'])))
        notify_subscribers(bot, user, new_events)
        connector.add_user_events(user['id'], new_events)
    connector.set_introspect_timestamp(user['id'], datetime.now().timestamp())
Example #6
0
def get_top_events(bot, update, args):
    connector = database.Connector()
    user = connector.get_user_by_chat_id(update.message.chat_id)
    top_events = connector.get_top_friend_events(user['id'])
    event_scores = dict(
        (event['event_id'], event['count']) for event in top_events)
    logging.info(repr(event_scores.keys()))
    found_events = timepad.find_events(event_scores.keys(), args)
    if len(top_events) > 0:
        found_events.sort(key=lambda event: -event_scores[event['id']])
    found_events = found_events[:3]
    message = '\n'.join(['Топ:'] + list(
        map(lambda event: timepad.format_event_descr(event), found_events)))
    bot.send_message(chat_id=update.message.chat_id,
                     text=message,
                     parse_mode='Markdown')
Example #7
0
def unsubscribe(bot, update, args):
    connector = database.Connector()
    if len(args) != 1:
        bot.send_message(chat_id=update.message.chat_id,
                         text='Use /unsubscribe <Telegram login>')
        return
    subscribed_to = args[0]
    if subscribed_to.startswith('@'):
        subscribed_to = subscribed_to[1:]
    user = connector.get_user_by_chat_id(update.message.chat_id)
    subscribed_id = connector.get_user_by_telegram(subscribed_to)
    if subscribed_id is None:
        bot.send_message(chat_id=update.message.chat_id,
                         text='Неизвестный пользователь')
        return
    connector.remove_subscription(subscribed_id, user['id'])
    bot.send_message(chat_id=update.message.chat_id, text='Подписка удалена')
Example #8
0
def notify_subscribers(bot, user, new_events):
    connector = database.Connector()
    subscribers = connector.get_subscribers(user['id'])
    events = timepad.get_events_data(new_events)

    if len(events) == 0:
        return
    for event in events:
        for subscriber_id in subscribers:
            subscriber = connector.get_user_by_id(subscriber_id)
            logging.info('Notifying {}'.format(str(subscriber)))
            bot.send_message(
                chat_id=subscriber['chat_id'],
                text='Твой друг @{} хочет посетить событие:\n{}'.format(
                    user['tg_name'], event['url']))
            photo = event['poster_image']['uploadcare_url']
            if photo.startswith('//'):
                photo = 'https:' + photo
            bot.send_photo(chat_id=subscriber['chat_id'], photo=photo)
Example #9
0
def button_more_callback(bot, update):
    query = update.callback_query
    parameters = {
        'access_statuses': "public",
        'fields': 'location',
        'limit': 100
    }
    update.message = query.message
    connector = database.Connector()
    user = connector.get_user_by_chat_id(update.message.chat.id)
    logging.info(user)
    if "more" in query.data:
        get_events_by_params(bot, update)
        return
    else:
        user_last_queries.clear()
    if "local" in query.data:
        if not check_token(bot, update, user):
            return
        city = connector.get_city(user['id'])
        if len(city) > 0:
            parameters['cities'] = city
    if "today" in query.data:
        date = datetime.today().strftime('%Y-%m-%d')
        parameters['starts_at_min'] = date + "T00:00:00+0300"
        parameters['starts_at_max'] = date + "T23:59:59+0300"
    if "my" in query.data:
        if not check_token(bot, update, user):
            return
        my_token = user['token']
        response = requests.get(timepad.API_URL +
                                '/introspect?token={0}'.format(my_token))
        user_info = json.loads(response.text)
        event_ids = [order['event']['id'] for order in user_info['orders']]
        parameters['event_ids'] = ','.join(str(id) for id in event_ids)

    get_events_by_params(bot, update, parameters)
Example #10
0
 def func_wrapper(bot, update, *args, **kwargs):
     connector = database.Connector()
     user = connector.get_user_by_chat_id(update.message.chat_id)
     if not check_token(bot, update, user):
         return
     func(bot, update, *args, **kwargs)