def update_round_message_task(chat_id, language_code, refresh): def get_text(t): return get(t, language_code) game_round = Round.objects.get(chat_id=chat_id) reply_markup = InlineKeyboardMarkup([ [ InlineKeyboardButton(get_text(Text.ROUND_REMOVE), callback_data='round:remove'), InlineKeyboardButton(get_text(Text.ROUND_FINISH), callback_data='round:finish'), InlineKeyboardButton("←", callback_data='round:prev'), InlineKeyboardButton("/next", callback_data='round:next'), ], ]) actors = game_round.get_actors() if not actors: return game_round.counter = game_round.counter % len(actors) counter = game_round.counter state = '' if game_round.hide: state = '[{}]'.format(get_text(Text.HIDED_ROUND_LIST)) round_counter = get_text( Text.ROUND_COUNTER).format(round_number=game_round.round_counter) text = '<b>{}</b> {state} #round\n\n{round_number} [{counter}/{total}]\n\n'.format( get_text(Text.ROUND_INDICATOR), state=state, round_number=round_counter, counter=counter + 1, total=len(actors), ) for index, actor in enumerate(actors): is_current = counter == index if is_current: text += '• {} ({}) ← {}\n'.format(actor.name, actor.value, get_text(Text.CURRENT)) elif not game_round.hide: text += '◦ {} ({})\n'.format(actor.name, actor.value) if refresh: try: bot.edit_message_text( text, chat_id=game_round.chat_id, message_id=game_round.message_id, parse_mode='HTML', reply_markup=reply_markup, ) except TelegramError: pass else: bot.delete_message(game_round.chat_id, game_round.message_id) message = bot.send_message(game_round.chat_id, text, parse_mode='HTML', reply_markup=reply_markup) game_round.message_id = message.message_id game_round.save()
def send_timer_massage(context: CallbackContext): context = context.job.context comment = context['comment'] timer = context['timer'] chat_id = context['chat_id'] encoded_comment = base64.b64encode(comment.encode()) reply_markup = InlineKeyboardMarkup([[ InlineKeyboardButton("⟳", callback_data='timer:new:{}:{}'.format( timer, encoded_comment.decode())), ]]) text = '{}秒倒计时结束'.format(timer) if len(comment) > 0: text += ': {}'.format(comment) try: bot.send_message(chat_id, text, reply_markup=reply_markup) except telegram.error.BadRequest: pass
def send_message_task(chat_id, text, reply_to=None, parse_mode='HTML', delete_after=None): try: sent = bot.send_message(chat_id, text, parse_mode, disable_web_page_preview=True, reply_to_message_id=reply_to) except telegram.error.TelegramError: logger.exception('Error on send message') return if delete_after and delete_after > 0: delete_message(chat_id, sent.message_id, delete_after)