Skip to content

Commit 5059c52

Browse files
working version
1 parent b01bda2 commit 5059c52

File tree

4 files changed

+57
-8
lines changed

4 files changed

+57
-8
lines changed

bot/Dockerfile

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
1-
FROM python:3.8
1+
FROM ubuntu:18.04
22

3+
RUN mkdir /usr/src/app
34
WORKDIR /usr/src/app
45

5-
COPY requirements.txt ./
6-
RUN pip install --no-cache-dir -r requirements.txt
6+
RUN apt-get update && apt-get install -y software-properties-common
7+
RUN apt-get update && apt-get install -y tesseract-ocr-all
8+
RUN apt-get update && apt-get install -y python3
9+
RUN apt-get update && apt-get install -y python3-pip
10+
RUN python3 -m pip install --upgrade pip
11+
RUN pip3 install Pillow
712

13+
COPY requirements.txt ./
14+
RUN pip3 install --no-cache-dir -r requirements.txt
815
COPY . .
916

10-
CMD [ "python", "./main.py" ]
17+
CMD ["python3", "main.py"]

bot/images/photo.jpg

17.3 KB
Loading

bot/main.py

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,59 @@
1-
from telegram.ext import Updater, CommandHandler
1+
from telegram import Update
2+
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters, CallbackContext
3+
import pytesseract
24
import os
5+
from PIL import Image
6+
import logging
7+
import traceback
38

9+
pytesseract.pytesseract.tesseract_cmd = '/usr/bin/tesseract' # ваш путь до tesseract
410

5-
def printreply(update, context) -> None:
6-
update.message.reply_text("Привет, как жизнь?")
11+
12+
def get_text_from_image(path):
13+
logging.info("getting image")
14+
img = Image.open(path)
15+
logging.info("image opened")
16+
return pytesseract.image_to_string(img, lang="rus+eng",timeout=30)
17+
18+
def echo_photo(update: Update, context: CallbackContext) -> None:
19+
print(update.message.photo)
20+
lst = update.message.photo
21+
logging.info("photo_size : " + str(len(lst)))
22+
result = "Что-то пошло не так, попробуйте позже"
23+
try:
24+
path = "images/photo.jpg"
25+
lst[-1].get_file().download(path, timeout=5);
26+
logging.info("download ok")
27+
result = get_text_from_image(path)
28+
logging.info("getting text ok")
29+
except Exception as e:
30+
logging.error(traceback.format_exc())
31+
try:
32+
update.message.reply_text(result)
33+
except Exception as e:
34+
update.message.reply_text("Текст не определен")
35+
36+
37+
def echo_no_photo(update: Update, context: CallbackContext) -> None:
38+
update.message.reply_text("Это не фото")
39+
40+
def help(update: Update, context: CallbackContext) -> None:
41+
update.message.reply_text("Пришлите фотографию, текст с которой вам нужен")
742

843

944
def main():
1045
updater = Updater(os.environ["MYFIRSTTGBOT"])
1146
dp = updater.dispatcher
12-
dp.add_handler(CommandHandler("hello", printreply))
47+
dp.add_handler(CommandHandler("start", help))
48+
dp.add_handler(CommandHandler("help", help))
49+
dp.add_handler(MessageHandler(Filters.photo & ~Filters.command, echo_photo))
50+
dp.add_handler(MessageHandler(~Filters.photo & ~Filters.command, echo_no_photo))
1351
updater.start_polling()
1452
updater.idle()
1553

1654

1755
if __name__ == "__main__":
1856
main()
57+
58+
59+
#/usr/src/app

bot/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
pytesseract
12
python-telegram-bot

0 commit comments

Comments
 (0)