-
Notifications
You must be signed in to change notification settings - Fork 30
/
barcode.py
67 lines (62 loc) · 2.01 KB
/
barcode.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
"""BarCode Generator
Command .barcode (your text)
By @snappy101
"""
import asyncio
import logging
import os
from datetime import datetime
from sample_config import Config
from userbot import bot
from userbot.util import admin_cmd
logging.basicConfig(format='[%(levelname) 5s/%(asctime)s] %(name)s: %(message)s',
level=logging.WARNING)
logger = logging.getLogger(__name__)
@bot.on(admin_cmd(pattern="barcode ?(.*)"))
async def _(event):
if event.fwd_from:
return
await event.edit("...")
start = datetime.now()
input_str = event.pattern_match.group(1)
message = "SYNTAX: `.barcode <long text to include>`"
reply_msg_id = event.message.id
if input_str:
message = input_str
elif event.reply_to_msg_id:
previous_message = await event.get_reply_message()
reply_msg_id = previous_message.id
if previous_message.media:
downloaded_file_name = await bot.download_media(
previous_message,
Config.TMP_DOWNLOAD_DIRECTORY,
)
m_list = None
with open(downloaded_file_name, "rb") as fd:
m_list = fd.readlines()
message = "".join(m.decode("UTF-8") + "\r\n" for m in m_list)
os.remove(downloaded_file_name)
else:
message = previous_message.message
else:
message = "SYNTAX: `.barcode <long text to include>`"
bar_code_type = "code128"
try:
bar_code_mode_f = barcode.get(
bar_code_type, message, writer=ImageWriter())
filename = bar_code_mode_f.save(bar_code_type)
await bot.send_file(
event.chat_id,
filename,
caption=message,
reply_to=reply_msg_id,
)
os.remove(filename)
except Exception as e:
await event.edit(str(e))
return
end = datetime.now()
ms = (end - start).seconds
await event.edit("Created BarCode in {} seconds".format(ms))
await asyncio.sleep(5)
await event.delete()