This repository has been archived by the owner on Jun 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
pdfbot.py
249 lines (194 loc) · 8.3 KB
/
pdfbot.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
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# -*- coding: utf-8 -*-
"""
Simple Bot to reply to Telegram messages.
First, a few handler functions are defined. Then, those functions are passed to
the Dispatcher and registered at their respective places.
Then, the bot is started and runs until we press Ctrl-C on the command line.
Usage:
Basic Echobot example, repeats messages.
Press Ctrl-C on the command line or send a signal to the process to stop the
bot.
"""
import os
import logging
import uuid
import shutil
import piexif
import img2pdf
from img2pdf import NegativeDimensionError, UnsupportedColorspaceError, ImageOpenError, \
JpegColorspaceError, PdfTooLargeError, AlphaChannelError, ExifOrientationError
from PyPDF2 import PdfFileMerger
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters, ConversationHandler
# Enable logging
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO)
logger = logging.getLogger(__name__)
SENDING = range(1)
def conv_image(file_name, args):
"""Convert an image to pdf."""
a4inpt = (img2pdf.mm_to_pt(210), img2pdf.mm_to_pt(297))
if args == "A4":
layout_fun = img2pdf.get_layout_fun(a4inpt)
else:
layout_fun = img2pdf.get_layout_fun()
try:
with open(file_name + ".pdf", "wb") as f:
f.write(img2pdf.convert(file_name, layout_fun=layout_fun))
except ExifOrientationError as e:
logger.warning("Error 1: {}".format(e))
piexif.remove(file_name)
with open(file_name + ".pdf", "wb") as f:
f.write(img2pdf.convert(file_name, layout_fun=layout_fun))
except (NegativeDimensionError, UnsupportedColorspaceError, ImageOpenError,
JpegColorspaceError, PdfTooLargeError, AlphaChannelError, ExifOrientationError) as e:
logger.warning("Error 2: {}".format(e))
raise
return True
def cleanup(file_name):
"""Cleanup files"""
os.remove(file_name)
os.remove(file_name + ".pdf")
logger.info("File removed")
def get_image(update, context, mode="Single"):
file_id = update.message.document.file_id
file_name = update.message.document.file_name
file_desc = update.message.caption
new_file = context.bot.get_file(file_id)
if mode == "Folder":
temp_name = os.path.join("/mnt/ramdisk", context.chat_data["idd"], file_id + file_name)
else:
temp_name = os.path.join("/mnt/ramdisk", file_id + file_name)
new_file.download(custom_path=temp_name)
logger.info("File saved")
return file_name, file_desc, temp_name
def join_pdfs(pdfs, idd):
merger = PdfFileMerger()
for pdf in pdfs:
merger.append(pdf)
merger.write(os.path.join("/mnt/ramdisk", idd, "result.pdf"))
merger.close()
# Define a few command handlers. These usually take the two arguments update and
# context. Error handlers also receive the raised TelegramError object in error.
def start(update, context):
"""Send a message when the command /start is issued."""
update.message.reply_text(
'Hi! You can send me images and I will convert them to pdf and send them back to you. '
'Please send the images as files (photos get compressed). '
'If you want the output to be A4 format use the caption "A4".')
def help(update, context):
"""Send a message when the command /help is issued."""
update.message.reply_text('Help!')
def echo(update, context):
"""Echo the user message."""
update.message.reply_text(update.message.text)
def convert_image(update, context):
"""Convert the sent image to pdf and send it back."""
file_name, file_desc, temp_name = get_image(update, context)
try:
conv_image(temp_name, file_desc)
context.bot.send_document(
chat_id=update.effective_chat.id, document=open(temp_name + ".pdf", 'rb'), filename=file_name + ".pdf")
logger.info("File send")
except (NegativeDimensionError, UnsupportedColorspaceError, ImageOpenError,
JpegColorspaceError, PdfTooLargeError, AlphaChannelError, ExifOrientationError) as e:
update.message.reply_text("Invalid Image: {}".format(e))
finally:
cleanup(temp_name)
def info_photo(update, context):
"""Send info that one should send images as files."""
update.message.reply_text("Please send images as files.")
return SENDING
def error(update, context):
"""Log Errors caused by Updates."""
logger.warning('Update "%s" caused error "%s"', update, context.error)
def join(update, context):
context.chat_data["idd"] = str(uuid.uuid4())
folder_name = os.path.join("/mnt/ramdisk", context.chat_data["idd"])
os.mkdir(folder_name)
context.chat_data["images"] = []
context.chat_data["folder"] = folder_name
context.chat_data["name"] = "result"
update.message.reply_text("Send images and /done when finished")
return SENDING
def add_image(update, context):
# Do something
file_name, file_desc, temp_name = get_image(update, context, mode="Folder")
try:
conv_image(temp_name, file_desc)
context.chat_data["images"].append(temp_name + ".pdf")
except (NegativeDimensionError, UnsupportedColorspaceError, ImageOpenError,
JpegColorspaceError, PdfTooLargeError, AlphaChannelError, ExifOrientationError) as e:
update.message.reply_text("Invalid Image: {}".format(e))
finally:
os.remove(temp_name)
return SENDING
def add_pdf(update, context):
# Do something
file_name, file_desc, temp_name = get_image(update, context, mode="Folder")
context.chat_data["images"].append(temp_name)
return SENDING
def set_title(update, context):
if len(context.args) != 0:
context.chat_data["name"] = context.args[0]
else:
update.message.reply_text("Please enter a name for the document.")
return SENDING
def done(update, context):
chat_data = context.chat_data
if chat_data is None:
chat_data = next(iter(context.job.context.dispatcher.chat_data.values()))
logger.info("timeout")
logger.info(chat_data)
if len(chat_data["images"]) != 0:
join_pdfs(chat_data["images"], chat_data["idd"])
context.bot.send_document(
chat_id=update.effective_chat.id,
document=open(os.path.join("/mnt/ramdisk", chat_data["idd"], "result.pdf"), 'rb'),
filename=chat_data["name"] + ".pdf")
logger.info("File send")
try:
shutil.rmtree(chat_data["folder"])
except OSError as e:
logger.warning("Error: {} : {}".format(chat_data["folder"], e.strerror))
update.message.reply_text("Finished")
chat_data.clear()
return ConversationHandler.END
def main():
"""Start the bot."""
# Create the Updater and pass it your bot's token.
# Make sure to set use_context=True to use the new context based callbacks
# Post version 12 this will no longer be necessary
TOKEN = os.getenv("TELEGRAM_TOKEN")
updater = Updater(TOKEN, use_context=True)
# Get the dispatcher to register handlers
dp = updater.dispatcher
conv_handler = ConversationHandler(
entry_points=[CommandHandler("join", join)],
states={
SENDING: [MessageHandler(Filters.document.category("image"), add_image),
MessageHandler(Filters.document.mime_type("application/pdf"), add_pdf),
CommandHandler("title", set_title),
],
ConversationHandler.TIMEOUT: [MessageHandler(Filters.all, done)],
},
fallbacks=[CommandHandler("done", done)],
conversation_timeout=60,
)
dp.add_handler(conv_handler)
# on different commands - answer in Telegram
dp.add_handler(CommandHandler("start", start))
dp.add_handler(CommandHandler("help", help))
# on noncommand i.e message - echo the message on Telegram
dp.add_handler(MessageHandler(Filters.text, echo))
dp.add_handler(MessageHandler(Filters.document.category("image"), convert_image))
dp.add_handler(MessageHandler(Filters.photo, info_photo))
# log all errors
dp.add_error_handler(error)
# Start the Bot
updater.start_polling()
# Run the bot until you press Ctrl-C or the process receives SIGINT,
# SIGTERM or SIGABRT. This should be used most of the time, since
# start_polling() is non-blocking and will stop the bot gracefully.
updater.idle()
if __name__ == '__main__':
main()