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
2
4
import os
5
+ from PIL import Image
6
+ import logging
7
+ import traceback
3
8
9
+ pytesseract .pytesseract .tesseract_cmd = '/usr/bin/tesseract' # ваш путь до tesseract
4
10
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 ("Пришлите фотографию, текст с которой вам нужен" )
7
42
8
43
9
44
def main ():
10
45
updater = Updater (os .environ ["MYFIRSTTGBOT" ])
11
46
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 ))
13
51
updater .start_polling ()
14
52
updater .idle ()
15
53
16
54
17
55
if __name__ == "__main__" :
18
56
main ()
57
+
58
+
59
+ #/usr/src/app
0 commit comments