def photo(message): try: fileID = message.photo[-1].file_id print ('fileID in the message =', fileID) file_info = bot.get_file(fileID) downloaded_file = bot.download_file(file_info.file_path) src = './tests/' + fileID with open(src, 'wb') as new_file: new_file.write(downloaded_file) print('start recognize: ', src) words = recognize.get_words_on_photo(src, fileID) translations = gtranslate.translateWords(words) print('recognized pairs', translations) dictionarySTR = '\n'.join("{0} @ {1}".format(key, val) for (key, val) in translations) send_message(message, dictionarySTR) except Exception as e: print(e) send_message(message, "Something went wrong")
def send_content(user, message, body): photos = bot.get_user_profile_photos(message.from_user.id).photos data = { 'event_type': 'new_message', 'payload': { 'timestamp': int(time.time()), 'msgid': str(message.message_id), 'conversation_id': 'c' + str(message.from_user.id) + str(user.api_postfix), 'sender': { 'id': str(message.from_user.id) + str(api_implementation_version), 'avatar': telegram_file_link % (bot_token, bot.get_file(photos[0][2].file_id).file_path), 'name': message.from_user.first_name.replace('.', '') + '.' + str(message.from_user.id), }, 'message': { 'type': 'text', 'text': body, } } } return send_data(data)
def handle_docs_audio(message): print(message.document) file_info = bot.get_file(message.document.file_id) file_url = 'https://api.telegram.org/file/bot' + str( config.TOKEN) + '/' + str(file_info.file_path) xl = pd.ExcelFile(file_url) print(xl.sheet_names) df1 = xl.parse('Sosi') print(df1)
def handle_text_doc(message): if state == 'quotation': document_id = message.document.file_id file_id_info = bot.get_file(document_id) downloaded_file = bot.download_file(file_id_info.file_path) file_name = message.document.file_name with open('downloaded_books/' + file_name, 'wb') as new_file: new_file.write(downloaded_file) bot.send_message(message.chat.id, 'Я загрузил вашу книгу, можете снова прислать Вашу цитату.') else: bot.send_message(message.chat.id, 'Извините, но Вы прислали это не вовремя.')
def send_photo(message): print(message) file_id = message.photo[-1].file_id print("file id:", file_id) file = bot.get_file(file_id) print("file:", file) try: file_path = file.file_path print("file path:", file_path) except Exception as e: print("get file path error:") print("\t", e)
def _create_review_page(hello_message, last_message, action, problems, user): photo = bot.get_user_profile_photos(user.id).photos[0][2] uploaded_user_photo = _upload_telegraph_file( bot.download_file(bot.get_file(photo.file_id).file_path)) root_node = [ { 'tag': 'p', 'children': [ 'Привет, %s' % user.first_name, ] }, { 'tag': 'img', 'attrs': { 'src': uploaded_user_photo } }, { 'tag': 'p', 'children': [ hello_message.text, ] }, { 'tag': 'p', 'children': [action.text] }, { 'tag': 'br', 'children': [] }, { 'tag': 'p', 'children': [last_message.text] }, ] problems_node_list = [] for problem in problems: problems_node_list.append({'tag': 'p', 'children': [problem.text]}) appearance = ProblemAppearance.objects.filter( problem_id=problem.id, type_action=action).first() if appearance: problems_node_list.append({ 'tag': 'p', 'children': [appearance.text] }) root_node = root_node[:4] + problems_node_list + root_node[4:] page = telegraph.create_page('Результаты типирования. ДКС.', root_node) return page['url']
def handle_docs_document(message): file_info = bot.get_file(message.photo[len(message.photo) - 1].file_id) chat_id = message.chat.id downloaded_file = bot.download_file(file_info.file_path) photo_to_change = io.BytesIO(downloaded_file) img = Image.open(photo_to_change) (width, height) = img.size drawing = ImageDraw.Draw(img) random_phrase = random.choice( list(open('lyrics/meat.txt', encoding="utf8"))) bot.send_message(chat_id, random_phrase) number_of_symbols = len(random_phrase) font = ImageFont.truetype('font.Ttf', int( (width / number_of_symbols) * 1.8)) font_color = (255, 255, 255) shadow_color = (0, 0, 0) x, y = (width / 20, height / 1.3) drawing.text((x - 1, y - 1), random_phrase, font=font, fill=shadow_color) drawing.text((x + 1, y - 1), random_phrase, font=font, fill=shadow_color) drawing.text((x - 1, y + 1), random_phrase, font=font, fill=shadow_color) drawing.text((x + 1, y + 1), random_phrase, font=font, fill=shadow_color) drawing.text((x, y), random_phrase, font=font, fill=font_color) bot.send_photo(chat_id, photo=img)
def get_file(request, file_id): url = telegram_file_link % (bot_token, bot.get_file(file_id).file_path) return render(request, 'video_file.html', context={'url': url})