def display_schedule(bot, payload, cmd, chat_id): def _send_company_offers(bot, chat_id): template = settings.JINJA_ENVIRONMENT.get_template('offers.md') msg = template.render({'finger': settings.SIGN_FINGER}) bot.sendMessage(chat_id, msg) bot.sendChatAction(chat_id, action='typing') schedule_id = cmd[9:len(cmd)] telegram_user_id = payload['message']['from']['id'] try: hall_image = draw_cinemahall(schedule_id) city_name_dict = {'cityName': u'Москва'.encode('utf-8')} url_encoded_dict = urllib.urlencode(city_name_dict) shorten_url = botan.shorten_url( 'https://kinohod.ru/widget/?{}#scheme_{}'.format( url_encoded_dict, schedule_id), settings.BOTAN_TOKEN, telegram_user_id) markup = InlineKeyboardMarkup( inline_keyboard=[[dict(text=settings.BUY_TICKET, url=shorten_url)] ]) _send_company_offers(bot, chat_id) bot.sendChatAction(chat_id, 'upload_photo') bot.sendPhoto(chat_id, ('hall.bmp', hall_image), reply_markup=markup) except: bot.sendMessage(chat_id, settings.SERVER_NOT_VALID)
def car(bot, update): short_url = botan.shorten_url(YA_MAPS_URL, BOTAN_IO, update.message.from_user.id) bot.sendMessage(update.message.chat_id, text=u'Маршрут построен! Выберите самый удобный ' \ u'из трех возможных маршрутов до пансионата "Университетский": %s' % short_url, reply_markup = REPLY_MARKUP)
def public_transport(bot, update): short_url = botan.shorten_url(HOW_TO_GET_THERE, BOTAN_IO, update.message.from_user.id) bot.sendMessage(update.message.chat_id, text=u'Электричка отправляется с Белорусского вокзала. ' \ u'Описание маршрутов общественного транспорта до пансионата "Университетский" ' \ u'на сайте ВШ: %s' % short_url, reply_markup = REPLY_MARKUP)
def display_movie_info_api(movie_id, telegram_user_id, next_url='/seance'): def get_data(name): return ', '.join([a.encode('utf-8') for a in html_data[name]]) url = settings.URL_MOVIES_INFO.format(movie_id, settings.KINOHOD_API_KEY) try: with contextlib.closing(urllib2.urlopen(url)) as jf: html_data = json.loads(jf.read()) except Exception as e: import logging logging.info(e.message) return None, None, None if isinstance(html_data, list): html_data = html_data[-1] if not html_data: return None, None, None movie_poster = _get_movie_poster(html_data['poster']) if ('trailers' in html_data and isinstance(html_data['trailers'], list) and len(html_data) > 0 and 'mobile_mp4' in html_data['trailers'][0]): kinohod_trailer_hash = ( html_data['trailers'][0]['mobile_mp4']['filename']) trailer_url = _get_movie_trailer_link(kinohod_trailer_hash) shorten_url = botan.shorten_url(trailer_url, settings.BOTAN_TOKEN, telegram_user_id) markup = InlineKeyboardMarkup(inline_keyboard=[[ dict(text=settings.TREILER, url=shorten_url), dict(text=settings.CHOOSE_SEANCE, callback_data=('{}{}num{}'.format( next_url, html_data['id'], settings.CINEMAS_TO_DISPLAY))), ]]) else: markup = InlineKeyboardMarkup(inline_keyboard=[[ dict(text=settings.CHOOSE_SEANCE, callback_data=( '{}{}num{}'.format(next_url, html_data['id'], 20))) ]]) template = settings.JINJA_ENVIRONMENT.get_template('movies_info.md') return (template.render({ 'title': html_data['title'], 'description': html_data['annotationFull'], 'duration': '{}'.format(html_data['duration']), 'genres': get_data('genres').decode('utf-8'), 'sign_actor': settings.SIGN_ACTOR, 'actors': get_data('actors').decode('utf-8'), 'producers': get_data('producers').decode('utf-8'), 'directors': get_data('directors').decode('utf-8') }), markup, movie_poster)
def echo(bot, update_id): # Request updates after the last update_id for update in bot.getUpdates(offset=update_id, timeout=10): # chat_id is required to reply to any message chat_id = update.message.chat_id update_id = update.update_id + 1 text = update.message.text print update if text: botan_token = config.botan_token # Token got from @botaniobot uid = update.message.from_user.id message_dict = update.message.to_dict() event_name = update.message.text print uid original_url = 'http://yandex.ru' # some url you want to send to user short_url = botan.shorten_url(original_url, botan_token, uid) print short_url # # Reply to the message bot.sendMessage(chat_id=chat_id, text=short_url) return update_id
def display_movie_info(movie_id, telegram_user_id=None, next_url='/seance', full=False): now = datetime.now() film = Film.get_by_id(str(movie_id)) telegram_user_id = telegram_user_id if telegram_user_id else 0 if not film: display_movie_info_api(movie_id, telegram_user_id, next_url='/seance') if film.poster: movie_poster = _get_movie_poster(film.poster.name) else: movie_poster = None if film.trailers and len(film.trailers) > 0: trailer = film.trailers[0].get() if trailer is not None: video_hash = trailer.videos[0].filename trailer_url = _get_movie_trailer_link(video_hash) else: trailer_url = settings.BASE_KINOHOD shorten_url = botan.shorten_url( str(telegram_user_id) + trailer_url, settings.BOTAN_TOKEN, telegram_user_id if telegram_user_id else 0) if film.premiereDateRussia and film.premiereDateRussia > now: markup = InlineKeyboardMarkup(inline_keyboard=[[ dict(text=settings.TREILER, url=shorten_url), dict(text=settings.NEAREST_SEANCES, callback_data='/future{}'.format(movie_id)) ]]) else: markup = InlineKeyboardMarkup(inline_keyboard=[[ dict(text=settings.TREILER, url=shorten_url), dict(text=settings.CHOOSE_SEANCE, callback_data=( '{}{}num{}'.format(next_url, film.kinohod_id, settings.CINEMAS_TO_DISPLAY))), ]]) else: if film.premiereDateRussia and film.premiereDateRussia > now: markup = InlineKeyboardMarkup(inline_keyboard=[[ dict(text=settings.NEAREST_SEANCES, callback_data='/future{}'.format(movie_id)) ]]) else: markup = InlineKeyboardMarkup(inline_keyboard=[[ dict(text=settings.CHOOSE_SEANCE, callback_data=( '{}{}num{}'.format(next_url, film.kinohod_id, 20))) ]]) Annotation = namedtuple('Annotation', ['title', 'link']) if full: template = settings.JINJA_ENVIRONMENT.get_template( 'movies_info_full.md') actors = film.actors ann_o = Annotation(film.annotationFull, '') return template.render({ 'title': film.title, 'description': ann_o, 'duration': film.duration if film.duration else None, 'premier': (film.premiereDateRussia.strftime('%d.%m.%Y') if film.premiereDateRussia else None), 'sign_calendar': settings.SIGN_CALENDAR, 'age': film.ageRestriction if film.ageRestriction else None, 'sign_genre': settings.SIGN_GENRE, 'kinder': settings.SIGN_CHILD_AGE, 'sign_time': settings.SIGN_ALARM, 'genres': ', '.join([a.get().name.encode('utf-8') for a in film.genres]).decode('utf-8'), 'sign_actor': settings.SIGN_ACTOR, 'actors': ', '.join([a.get().name.encode('utf-8') for a in actors]).decode('utf-8') if actors else None, 'directors': ', '.join([a.get().name.encode('utf-8') for a in film.directors ]).decode('utf-8') if film.directors else None, 'sign_producer': settings.SIGN_PRODUCER, }), markup, movie_poster else: template = settings.JINJA_ENVIRONMENT.get_template('movies_info.md') actors = (film.actors[:3] if film.actors and len(film.actors) > 3 else film.actors) ann_o = Annotation( film.annotationShort, 'Подробнее: /fullinfo{}'.decode('utf-8').format(movie_id)) return template.render({ 'title': film.title, 'description': ann_o, 'sign_genre': settings.SIGN_GENRE, 'genres': ', '.join([a.get().name.encode('utf-8') for a in film.genres ]).decode('utf-8') if film.genres else None, 'sign_actor': settings.SIGN_ACTOR, 'actors': ', '.join( [a.get().name.encode('utf-8') for a in actors if a.get()]).decode('utf-8') if actors else None, 'directors': ', '.join([ a.get().name.encode('utf-8') for a in film.directors if a.get() ]).decode('utf-8') if film.directors else None, 'sign_producer': settings.SIGN_PRODUCER, }), markup, movie_poster
def get_link(uid): return botan.shorten_url('https://telegram.me/storebot?start=rog_bot', tkn, uid)