1- from telegram import Update
2- from utils .logging import telegram_bot
3-
4- import telegram
5- from telegram .ext import (
6- CallbackContext
7- )
8- import requests
1+ import json
92from typing import Dict
10- from config .settings import API_URL , API_KEY , API_UPLOAD_PATH
11-
123
4+ import requests
5+ from config .settings import API_KEY , API_PREDICT_PATH , API_UPLOAD_PATH , API_URL
6+ from telegram import InlineKeyboardButton , InlineKeyboardMarkup , Update
7+ from telegram .ext import CallbackContext
8+ from utils .logging import telegram_bot
139
1410
1511def send_photo_to_api (files : Dict ):
@@ -24,93 +20,187 @@ def send_photo_to_api(files: Dict):
2420 """
2521 data = {"source" : "telegram" }
2622 headers = {"X-Api-Key" : API_KEY }
27- telegram_bot .debug (f "Sending the file object to the API." )
23+ telegram_bot .debug ("Sending the file object to the API." )
2824 try :
29- r = requests .post (API_URL + API_UPLOAD_PATH , files = files , data = data , headers = headers )
25+ r = requests .post (
26+ API_URL + API_UPLOAD_PATH , files = files , data = data , headers = headers
27+ )
3028 if r .status_code == 201 :
3129 telegram_bot .info (f"File sucessfully uploaded: { r .text } " )
3230 except Exception as e :
3331 telegram_bot .error (f"There is an error when send the file: { e } ." )
3432
3533
36-
3734def start (update : Update , context : CallbackContext ) -> None :
3835 """
3936 Name: start
4037 Description:
41- Start callable function when /start command is used on the telegram bot.
38+ Start callable function when /start command is used on the telegram bot.
4239 Send a reply when using that command.
43- Inputs:
40+ Inputs:
4441 :update: type(Update): update handler object.
4542 :context: type(CallbackContext).
4643 Outputs:
47- None
44+ None
4845 """
49- context .bot .send_message (chat_id = update .effective_chat .id , text = "Welcome to mairror! Do you need to guess your age?" )
46+ context .bot .send_message (
47+ chat_id = update .effective_chat .id ,
48+ text = "Welcome to mairror! Do you need to guess your age?" ,
49+ )
50+
5051
5152def text (update : Update , context : CallbackContext ) -> None :
5253 """
5354 Name: text
5455 Description:
55- When a text message is sent in the telegram bot, the context send that message.
56- Inputs:
56+ When a text message is sent in the telegram bot, the context send that message.
57+ Inputs:
5758 :update: type(Update): update handler object.
5859 :context: type(CallbackContext).
5960 Outputs:
60- None
61+ None
6162 """
62- context .bot .send_message (chat_id = update .effective_chat .id , text = "Sorry, I didn't understand you." )
63+ context .bot .send_message (
64+ chat_id = update .effective_chat .id , text = "Sorry, I didn't understand you."
65+ )
66+
6367
6468def unknown (update : Update , context : CallbackContext ) -> None :
6569 """
6670 Name: unknown
6771 Description:
68- When an unknown command is used the bot is replied with this message.
69- Inputs:
72+ When an unknown command is used the bot is replied with this message.
73+ Inputs:
7074 :update: type(Update): update handler object.
7175 :context: type(CallbackContext).
7276 Outputs:
73- None
77+ None
7478 """
75- context .bot .send_message (chat_id = update .effective_chat .id , text = "Sorry, I didn't understand that command." )
79+ context .bot .send_message (
80+ chat_id = update .effective_chat .id ,
81+ text = "Sorry, I didn't understand that command." ,
82+ )
7683
7784
7885def help_command (update : Update , context : CallbackContext ) -> None :
7986 """
8087 Name: unknown
8188 Description:
82- When used the /help command, you can get the command helper for the telegram bot.
83- Inputs:
89+ When used the /help command, you can get the command helper for the telegram bot.
90+ Inputs:
8491 :update: type(Update): update handler object.
8592 :context: type(CallbackContext).
8693 Outputs:
87- None
94+ None
8895 """
89- update .message .reply_text ('''
96+ update .message .reply_text (
97+ """
9098<b>Mairror Bot:</b>
9199
92100You can find <a href="https://github.com/mairror/telegram-bot/blob/main/README.md">here</a> this bot's documentation.
93101Authors of this bot:
94102
95103 - <a href="https://www.linkedin.com/in/%E2%9C%85-borja-l-422666a9">Borja</a>
96104 - <a href="https://www.linkedin.com/in/aacecan">Alex</a>
97- ''' , parse_mode = "HTML" , disable_web_page_preview = True )
105+ """ ,
106+ parse_mode = "HTML" ,
107+ disable_web_page_preview = True ,
108+ )
109+
110+
111+ def build_text_prediction (prediction : Dict ):
112+ newline = "\n "
113+ text = f"""
114+ <b>PREDICTION:</b>\n
115+ { newline .join ([f"Face { count + 1 } : (Age: { pred ['age' ]} ), (Gender: { pred ['gender' ]} )"
116+ for count , pred in enumerate (prediction ["predictions" ]) ])}
117+ """
118+ return text
119+
120+
121+ def predict (image ):
122+ data = {"image_id" : image }
123+ print (data )
124+ headers = {"X-Api-Key" : API_KEY }
125+ telegram_bot .debug (f"Predict image { image } ." )
126+ try :
127+ r = requests .post (
128+ API_URL + API_PREDICT_PATH , data = json .dumps (data ), headers = headers
129+ )
130+ if r .status_code == 200 :
131+ telegram_bot .info (f"Sucessfully predicted: { r .text } " )
132+ return build_text_prediction (json .loads (r .text ))
133+ else :
134+ telegram_bot .error (f"Error querying the API: { r .text } ." )
135+ return "There was an error predicting your image."
136+ except Exception as e :
137+ telegram_bot .error (f"There is an error when get tyhe prediction: { e } ." )
138+
139+
140+ def keyboard ():
141+ keyboard = [
142+ [
143+ InlineKeyboardButton ("Yes!!!" , callback_data = "yes" ),
144+ InlineKeyboardButton ("No" , callback_data = "no" ),
145+ ],
146+ ]
147+
148+ reply_markup = InlineKeyboardMarkup (keyboard )
149+ return reply_markup
150+
151+
152+ def button (update : Update , context : CallbackContext ) -> None :
153+ """
154+ Name: photo
155+ Description:
156+ Used to send a photo into the API when you uploaded in the telegram bot.
157+ Inputs:
158+ :update: type(Update): update handler object.
159+ :context: type(CallbackContext).
160+ Outputs:
161+ None
162+ """
163+ query = update .callback_query
164+ query .answer ()
165+ if query .data == "yes" :
166+ query .edit_message_text (text = "I'm a great guesser!" )
167+ else :
168+ query .edit_message_text (
169+ text = "Sorry, I'll do the best of me. I need more images to improve the prediction."
170+ )
98171
99172
100173def photo (update : Update , context : CallbackContext ) -> None :
101174 """
102175 Name: photo
103176 Description:
104- Used to send a photo into the API when you uploaded in the telegram bot.
105- Inputs:
177+ Used to send a photo into the API when you uploaded in the telegram bot.
178+ Inputs:
106179 :update: type(Update): update handler object.
107180 :context: type(CallbackContext).
108181 Outputs:
109- None
182+ None
110183 """
184+ image = f"{ update .message .photo [- 1 ].file_unique_id } _{ update .message .chat .id } .jpg"
111185 files = {
112186 "file" : (
113187 # AQADFrkxG-3bCVFy_3235057.jpg
114- f"{ update .message .photo [- 1 ].file_unique_id } _{ update .message .chat .id } .jpg" ,
115- bytes (update .message .photo [- 1 ].get_file ().download_as_bytearray ()))}
188+ image ,
189+ bytes (update .message .photo [- 1 ].get_file ().download_as_bytearray ()),
190+ )
191+ }
192+
116193 send_photo_to_api (files )
194+
195+ context .bot .send_chat_action (chat_id = update .effective_chat .id , action = "typing" )
196+
197+ prediction_result = predict ("raw/" + image )
198+ if prediction_result :
199+ context .bot .send_message (
200+ chat_id = update .effective_chat .id ,
201+ text = prediction_result ,
202+ parse_mode = "HTML" ,
203+ disable_web_page_preview = True ,
204+ )
205+ reply_markup = keyboard ()
206+ update .message .reply_text ("Have I guessed your age?" , reply_markup = reply_markup )
0 commit comments