Exemple #1
0
def continent_handler(bot, update):
    user_id = update.message.from_user.id
    chat_id = update.message.chat_id
    text = update.message.text

    if text == BACK:
        with Shelve() as sh:
            cont_id = sh.get(chat_id, CONTINENT)
    else:
        found = [cont for cont in get_continent_name_id() if cont[0] == text]
        if not found:
            return STATE_MENU

        cont_id = found[0][1]
        with Shelve() as sh:
            sh.set(chat_id, CONTINENT, cont_id)

    reply_keyboard = [[KeyboardButton(d)] for d in filter_region_name(cont_id)]
    reply_keyboard.append([KeyboardButton(BACK), KeyboardButton(MENU)])

    bot.sendMessage(chat_id,
                    text=u'Выберите регион',
                    reply_markup=ReplyKeyboardMarkup(reply_keyboard,
                                                     resize_keyboard=True))

    report.track_screen(user_id, 'continent/%s' % str(cont_id))
    return STATE_TYPE
Exemple #2
0
def move_handler(bot, update):
    query = update.callback_query
    chat_id = query.message.chat_id
    user_id = query.message.from_user.id

    move = query.data
    report.track_screen(query.message.from_user.id, 'move_' + move)
    db.set(chat_id, 'move', move)

    image_path = db.get(chat_id, 'image_path')
    image_title = db.get(chat_id, 'image_title')

    mode = db.get(chat_id, 'mode', MODE_PANO)
    if image_path and os.path.exists(image_path):
        send_photo_parts(bot, chat_id, user_id, image_path, image_title)

        text = 'You can move pieces row up or down:' \
               '\n---\nВы можете передвинуть ряд фото выше или ниже:'
        bot.edit_message_text(text=text,
                              parse_mode=ParseMode.MARKDOWN,
                              chat_id=chat_id,
                              message_id=query.message.message_id)

        bot.send_message(text=text,
                         parse_mode=ParseMode.MARKDOWN,
                         chat_id=chat_id,
                         message_id=query.message.message_id,
                         reply_markup=move_reply(mode, move))
    else:
        bot.send_message(text='Please, upload a photo',
                         parse_mode=ParseMode.MARKDOWN,
                         chat_id=chat_id,
                         message_id=query.message.message_id)
Exemple #3
0
def region_handler(bot, update):
    user_id = update.message.from_user.id
    chat_id = update.message.chat_id
    text = update.message.text

    if text == BACK:
        with Shelve() as sh:
            region_id = sh.get(chat_id, REGION)
            sh.set(chat_id, 'last_map', '')
    else:
        found = [cont for cont in get_region_name_id() if cont[0] == text]
        if not found:
            return STATE_MENU
        region_id = found[0][1]
        with Shelve() as sh:
            sh.set(chat_id, REGION, region_id)
            sh.set(chat_id, 'last_map', '')

    reply_keyboard = [[KeyboardButton(d)] for d in filter_type_name(region_id)]
    reply_keyboard.append([KeyboardButton(BACK), KeyboardButton(MENU)])

    bot.sendMessage(chat_id,
                    text=u'Выберите тип',
                    reply_markup=ReplyKeyboardMarkup(reply_keyboard,
                                                     resize_keyboard=True))

    report.track_screen(user_id, 'region/%s' % str(region_id))
    return STATE_MAP
Exemple #4
0
def type_handler(bot, update):
    chat_id = update.message.chat_id
    user_id = update.message.from_user.id
    text = update.message.text

    try:
        found = [cont for cont in get_map_type_id() if cont[0] == text]
        if not found:
            return STATE_MENU

        type_id = found[0][1]
        with Shelve() as sh:
            sh.set(chat_id, MAPTYPE, type_id)

        timestamp = datetime.utcnow()

        with Shelve() as sh:
            sh.set(chat_id, MAPTYPE, type_id)

            map_id = get_map_id(sh.get(chat_id, REGION), type_id)
            sh.set(chat_id, MAPID, map_id)

        report.track_screen(user_id, 'type/%s' % str(type_id))
        return send_map(bot, update, map_id, timestamp)

    except Exception, err:
        bot.sendMessage(chat_id,
                        text=u"Что-то пошло не так. Начать сначала: /start")
Exemple #5
0
def manual_location_handler(bot, update):

    chat_id = update.message.chat_id
    user_id = update.message.from_user.id
    text = update.message.text

    try:
        if text == MENU:
            return start_handler(bot, update)

        latidude, longitude = text.split(',')

        region_id = get_region_by_location(float(latidude), float(longitude))
        if not region_id:
            bot.sendMessage(chat_id, text=u'Извините, у меня нет такой карты')
            report.track_screen(user_id, 'location/not_found')
            return STATE_LOCATION

        report.track_screen(user_id, 'location/region/%s' % str(region_id))

        update.message.text = get_region_name_id()[region_id - 1][0]
        return region_handler(bot, update)

    except Exception, err:
        return command_location_handler(bot, update)
Exemple #6
0
def error_handler(bot, update, error):

    if update:
        user_id = update.message.from_user.id
        report.track_screen(user_id, '/handle_error')

    log.error('type_handler error: %s' % str(error))
    log.error(exception_info())
Exemple #7
0
def photo_handler(bot, update):
    if len(update.message.photo):
        update.message.reply_text(
            'Check \"send file\" not \"photo\" to better quality\n'
            'Выберите \"отослать файл\", а не фотографию, чтобы не ухудшилось качество'
        )
        report.track_screen(update.message.from_user.id,
                            'check_file_not_photo_message')
Exemple #8
0
def command_location_handler(bot, update):

    chat_id = update.message.chat_id
    user_id = update.message.from_user.id
    reply_keyboard = [[KeyboardButton(MENU)]]
    bot.sendMessage(
        chat_id,
        text=u'Чтобы узнать погоду по координатам введите: <широта>,<долгота>',
        reply_markup=ReplyKeyboardMarkup(reply_keyboard, resize_keyboard=True))
    report.track_screen(user_id, 'location')
    return STATE_LOCATION
Exemple #9
0
def next_handler(bot, update):

    chat_id = update.message.chat_id
    report.track_screen(update.message.from_user.id, 'next')

    with Shelve() as sh:
        p = sh.get(chat_id, 'last_map')
        map_id = sh.get(chat_id, MAPID)

    timestamp = get_next_timestamp_by_path(p)
    return send_map(bot, update, map_id, timestamp)
Exemple #10
0
def refresh_handler(bot, update):
    user_id = update.message.from_user.id
    chat_id = update.message.chat_id
    timestamp = datetime.utcnow()

    with Shelve() as sh:
        map_id = sh.get(chat_id, MAPID)
        report.track_screen(user_id, 'refresh')
        return send_map(bot, update, map_id, timestamp)

    return STATE_MAP
Exemple #11
0
def start_handler(bot, update):

    report.track_screen(update.message.from_user.id, 'start')

    text = 'Hello!\n' \
           'Send me a picture as a file, I split it into parts for Instagram. ' \
           'I make 3 parts for 2x3 photo, >3 for panoramic photo.\n-----\n' \
           'Привет!\n' \
           'Пришли мне фотографию и я разделю ее на части для Инстаграмма. ' \
           'Я сделаю 3 части из обычного фото 2х3 и больше 3-х для панорамной фотографии\n'

    update.message.reply_text(text, parse_mode=ParseMode.MARKDOWN)
Exemple #12
0
def location(bot, update):
    chat_id = update.message.chat_id
    user_id = update.message.from_user.id

    latidude = update.message.location['latitude']
    longitude = update.message.location['longitude']

    region_id = get_region_by_location(latidude, longitude)
    report.track_screen(user_id, 'location/region/%s' % str(region_id))

    update.message.text = get_region_name_id()[region_id - 1][0]
    return region_handler(bot, update)
Exemple #13
0
def legend_handler(bot, update):
    chat_id = update.message.chat_id

    with Shelve() as sh:
        map_id = sh.get(chat_id, MAPID)
        if map_id:
            info = get_legend(map_id)
            bot.sendMessage(chat_id,
                            text=info,
                            reply_markup=ReplyKeyboardMarkup(
                                show_map_reply_markup, resize_keyboard=True))
            report.track_screen(update.message.from_user.id,
                                'legend/%s' % str(map_id))
            return SHOW_MAP

    return STATE_MENU
Exemple #14
0
def split_handler(bot, update):

    chat_id = update.message.chat_id
    user_id = update.message.from_user.id
    image_title = update.message.document.file_name
    image_title = ''.join(image_title.split('.')[:-1]).encode('utf-8')
    image_file = bot.get_file(update.message.document.file_id)

    try:
        move = db.get(chat_id, 'move', MOVE_CENTER)

        image_path = tempfile.mktemp()
        with open(image_path, 'wb') as f:
            image_file.download(out=f)

        send_photo_parts(bot, chat_id, user_id, image_path, image_title)

    except Exception, err:
        log.error('slice9_handler error: {}'.format(err))
        update.message.reply_text('Error: {}'.format(err))
        report.track_screen(user_id, 'panorama/slice9_handler')
Exemple #15
0
def send_photo_parts(bot, chat_id, user_id, image_path, image_title):

    move = db.get(chat_id, 'move', MOVE_CENTER)
    mode = db.get(chat_id, 'mode', MODE_PANO)
    if mode == MODE_9:
        report.track_screen(user_id, 'nine_piece')
        pieces = slice_9(image_path, image_title)
    else:
        report.track_screen(user_id, 'panorama')
        pieces = slice_pano(image_path, image_title, move)
        report.track_event(user_id, 'panorama pieces', str(len(pieces)))

    try:
        if pieces:
            for piece in pieces:
                try:
                    with open(piece, 'rb') as f:
                        caption = 'Part №{0} of {1}'.format(
                            pieces.index(piece) + 1, image_title)
                        bot.send_document(chat_id, document=f, caption=caption)
                except Exception, err:
                    report.track_screen(user_id,
                                        'panorama/send_piece_exception')
                    log.error('send_piece_exception: {}'.format(err))

            db.set(chat_id, 'image_path', image_path)
            db.set(chat_id, 'image_title', image_title)

    finally:
        for p in pieces:
            os.unlink(p)
Exemple #16
0
def select_continent(bot, update):

    user_id = update.message.from_user.id
    chat_id = update.message.chat_id
    text = update.message.text

    if text == BACK or text == '/manual' or text == u'Выбрать карту':

        reply_keyboard = [[KeyboardButton(d[0])]
                          for d in get_continent_name_id()]
        reply_keyboard.append([KeyboardButton(BACK), KeyboardButton(MENU)])

        bot.sendMessage(chat_id,
                        text=u'Выберите континент',
                        reply_markup=ReplyKeyboardMarkup(reply_keyboard,
                                                         resize_keyboard=True))
        report.track_screen(user_id, 'select_continent')
        return STATE_REGION
    else:
        # Предыдущая карта
        return refresh_handler(bot, update)

    return STATE_MENU
Exemple #17
0
def send_map(bot, update, map_id, timestamp):

    chat_id = update.message.chat_id
    user_id = update.message.from_user.id

    path, info = get_map(map_id, timestamp)
    log.info('path %s' % path)

    reply_keyboard = [[
        KeyboardButton(PREVIOUS_MAP),
        KeyboardButton(REFRESH),
        KeyboardButton(NEXT_MAP)
    ], [KeyboardButton(BACK), KeyboardButton(MENU)]]

    with Shelve() as sh:
        last_path = sh.get(chat_id, 'last_map')

        if path and last_path == path:
            bot.sendMessage(chat_id,
                            text=u'<обновление отсутствует>',
                            reply_markup=ReplyKeyboardMarkup(
                                reply_keyboard, resize_keyboard=True))

            report.track_screen(user_id, 'map/%s/no_update' % (map_id))
        else:

            if path:
                with open(path.encode('utf-8'), 'rb') as image:
                    bot.sendPhoto(chat_id,
                                  image,
                                  caption=info.encode('utf-8'),
                                  reply_markup=ReplyKeyboardMarkup(
                                      show_map_reply_markup,
                                      resize_keyboard=True))

                    report.track_screen(user_id, 'map/%s' % str(map_id))
                    sh.set(chat_id, 'last_map', path)
            else:
                bot.sendMessage(chat_id,
                                text=info + u'\n<изображение отсутствует>',
                                reply_markup=ReplyKeyboardMarkup(
                                    show_map_reply_markup,
                                    resize_keyboard=True))
                report.track_screen(user_id, 'map/%s/no_image' % str(map_id))
    return SHOW_MAP