forked from Kylmakalle/heroku-telegram-bot
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbot.py
50 lines (40 loc) · 1.65 KB
/
bot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import convertapi
import os
import telebot
import tempfile
import requests
from io import BytesIO
token = os.environ['TELEGRAM_TOKEN']
convertapi.api_secret = os.environ['CONVERTAPI_SECRET']
bot = telebot.TeleBot(token)
@bot.message_handler(commands=['start'])
def start_message(message):
bot.send_message(message.chat.id, 'Please send me your files')
@bot.message_handler(content_types=['document', 'audio', 'photo', 'video'])
def convert_to_pdf(message):
#print(message)
bot.send_message(message.chat.id, 'Converting...')
send_photo_warning = False
content_type = message.content_type
documents = getattr(message, content_type)
if not isinstance(documents, list):
documents = [documents]
for doc in documents:
file_id = doc.file_id
file_info = bot.get_file(file_id)
#print(file_info)
file_url = 'https://api.telegram.org/file/bot{0}/{1}'.format(token, file_info.file_path)
file_data = BytesIO(requests.get(file_url).content)
try:
file_name = doc.file_name
except:
file_name = file_info.file_path.replace("/", "_")
send_photo_warning = True
upload_io = convertapi.UploadIO(file_data, filename=file_name)
converted_result = convertapi.convert('pdf', {'File': upload_io})
converted_files = converted_result.save_files(tempfile.gettempdir())
for file in converted_files:
bot.send_document(message.chat.id, open(file, 'rb'))
if send_photo_warning:
bot.send_message(message.chat.id, 'You sent this file as a photo. If you require better quality, please send it as a document.')
bot.polling()