def drawer(): if request.method == 'GET': file_id = request.args.get('file_id', None) chat_id = request.args.get('chat_id', None) message_id = request.args.get('message_id', None) submission_id = request.args.get('submission_id', None) file_info = bot.get_file(file_id) response = requests.get( 'https://api.telegram.org/file/bot{0}/{1}'.format( settings.BOT_TOKEN, file_info.file_path)) return render_template('drawing.html', image_bytes=base64.b64encode( response.content).decode('utf-8'), chat_id=chat_id, message_id=message_id, submission_id=submission_id) if request.method == 'POST': data = request.values.to_dict() image_str = data.get('imgBase64', None) chat_id = data.get('chat_id', None) message_id = data.get('message_id', None) submission_id = data.get('submission_id', None) image_bytes = base64.b64decode(image_str[image_str.index(';base64,') + 8:]) if message_id and submission_id: bot.edit_message_media(InputMediaPhoto(image_bytes), chat_id, message_id) SubmissionReviewMessage(submission_id, chat_id, message_id, created_utc=datetime.now( timezone.utc)).save() else: bot.send_photo(chat_id, image_bytes) return 'ok'
def send_telegram_image(chat: Chat, image_url: str, text: str, parse_mode: ParseMode = telegram.ParseMode.MARKDOWN, **kwargs): if not bot: log.warning("No telegram token. Skipping") return log.info(f"Telegram: sending the image: {image_url} {text}") return bot.send_photo(chat_id=chat.id, photo=image_url, caption=text[:1024], parse_mode=parse_mode, **kwargs)
def send_telegram_image(chat: Chat, image_url: str, text: str, parse_mode: ParseMode = telegram.ParseMode.HTML, **kwargs): if not bot: log.warning("No telegram token. Skipping") return log.info(f"Telegram: sending the image: {image_url} {text}") try: return bot.send_photo(chat_id=chat.id, photo=image_url, caption=text[:1024], parse_mode=parse_mode, **kwargs) except telegram.error.TelegramError as ex: log.warning(f"Telegram error: {ex}")
def send_photo(cid, photo): bot.send_photo(cid, photo=open(photo, 'rb'))
def send_photo(user_id, path): return bot.send_photo(user_id, BuildAndSendGantt.read_file(path), parse_mode="HTML")