-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathBot.py
More file actions
105 lines (78 loc) · 3.42 KB
/
Copy pathBot.py
File metadata and controls
105 lines (78 loc) · 3.42 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
from telegram.ext import Updater, MessageHandler, Filters, CommandHandler
import logging
import subprocess
import re
import os
from Secrets import TOKEN
logging.basicConfig(
format=u'%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO)
logger = logging.getLogger(__name__)
def convert_to(folder, source):
args = ['libreoffice', '--headless', '--convert-to', 'pdf', '--outdir', folder, source]
process = subprocess.run(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=None)
file = re.search('-> (.*?) using filter', process.stdout.decode())
if file is None:
raise Process(process.stdout.decode())
else:
return file.group(1)
class Process(Exception):
def __init__(self, output):
self.output = output
def start(bot, update):
bot.sendMessage(chat_id=update.message.chat_id,
text='Hello! Send me a DOC or DOCX file and I will try to convert it to PDF :)')
def error(bot, update, e):
logger.warning(u'Update "%s" caused error "%s"' % (update, e))
update.message.reply_text(e)
def listener(bot, update):
bot.sendMessage(chat_id=update.message.chat_id,
text="I'm sorry, I don't understand you!"
"\nI'm able to reply with a PDF when you send me a DOC or DOCX file")
def check_document(file_name):
return file_name.endswith('.docx') or file_name.endswith('.doc')
def get_destination_path(path, file_url):
down_file = os.path.join(path, os.path.basename(file_url))
new_file = os.path.join(path, os.path.splitext(
os.path.basename(file_url))[0]) + '.pdf'
return down_file, new_file
def document_saver(bot, update):
if update.message.document and check_document(update.message.document.file_name):
last_document = update.message.document
try:
doc_file = bot.getFile(last_document.file_id)
my_path = os.path.abspath(os.path.dirname(__file__))
down, new = get_destination_path(my_path, update.message.document.file_name)
doc = doc_file.download(down)
convert_to(my_path, update.message.document.file_name)
bot.sendMessage(chat_id=update.message.chat_id,
text='Converting "%s"!' % update.message.document.file_name)
bot.send_document(chat_id=update.message.chat_id,
document=open(new, 'rb'))
if os.path.exists(doc):
os.remove(doc)
if os.path.exists(new):
os.remove(new)
except Exception as e:
logger.error('Error:%s' % e)
bot.sendMessage(chat_id=update.message.chat_id,
text="Error! :(")
else:
bot.sendMessage(chat_id=update.message.chat_id,
text="I'm only able to convert DOCX and DOC files!")
def main():
# Create handler
updater = Updater(TOKEN)
dp = updater.dispatcher
# Log errors
dp.add_error_handler(error)
# Handle messages
dp.add_handler(CommandHandler('start', start))
dp.add_handler(MessageHandler(Filters.document, document_saver))
dp.add_handler(MessageHandler(Filters.text, listener))
# Start the Bot
updater.start_polling()
logger.info('Bot started')
updater.idle()
if __name__ == '__main__':
main()