-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbot.py
43 lines (34 loc) · 1.47 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
# -*- coding: utf-8 -*-
import os
import io
import datetime
import logging
from aiogram import Bot, Dispatcher, executor, types
from PIL import Image
import pyzbar.pyzbar as pyzbar
logging.basicConfig(level=logging.INFO)
token = os.getenv('ZEBRA_TOKEN', '')
bot = Bot(token)
dp = Dispatcher(bot)
@dp.message_handler(commands=['start', 'help'])
async def start(message: types.Message):
start_message = 'I am Little Zebra, barcode scanner. Send me image with barcode to decode it.\n' \
'[Github](https://github.com/gurza/little-zebra)'
await message.answer(start_message, parse_mode='Markdown')
@dp.message_handler(content_types=[types.ContentType.PHOTO])
async def scan(message: types.Message):
image_data = await bot.download_file_by_id(message.photo[0].file_id)
image = Image.open(io.BytesIO(image_data.getvalue()))
decoded_objects = pyzbar.decode(image)
result_message = 'Could not find the barcode'
if len(decoded_objects):
result_message = ",".join([
"{data} ({type})".format(data=obj.data.decode("utf-8"), type=obj.type)
for obj in decoded_objects
])
logging.info('{now} @{user}: {result}'.format(
now=datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'), user=message.chat.username, result=result_message))
await message.reply(result_message)
if __name__ == '__main__':
logging.info('little-zebra - {ver}'.format(ver="1.0.0"))
executor.start_polling(dp, skip_updates=True)