Skip to content

Commit

Permalink
add function to send request for message and photo to manager error m…
Browse files Browse the repository at this point in the history
…essage when send photo and send message without image if with image it doesn't

add trace of image to be send to try to understand why in some case the image is not send
fix error in command again
  • Loading branch information
giloser committed May 31, 2024
1 parent 603247f commit 0526ffe
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 57 deletions.
114 changes: 58 additions & 56 deletions octoprint_telegram/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2011,23 +2011,20 @@ def _send_msg(
self._logger.debug("cam conf URL : " + str(url))
if image_data == None:
image_data = self.take_image(url,config)
self._logger.debug ("take_image return data : %s",image_data)
else:
image_data2 = self.take_image(url,config)
self._logger.debug ("take_image return data : %s",image_data2)
if str(image_data2) != "":
self._logger.debug(
"Image for " + str(config.name)
)
files = {
"photo": ("image.jpg", image_data2)
"photo": ("image.jpg", image_data2, 'image/jpeg')
}
data2 = data
data2["caption"] = ""
r = requests.post(
self.bot_url + "/sendPhoto",
files=files,
data=data2,
proxies=self.getProxies(),
)
r = self.send_request(self.bot_url,"/sendPhoto",files,data2,self.getProxies())
else:
self._logger.debug(
"no image " + str(config.name)
Expand Down Expand Up @@ -2100,16 +2097,11 @@ def _send_msg(
"Image for " + str(li.get("name"))
)
files = {
"photo": ("image.jpg", image_data2)
"photo": ("image.jpg", image_data2 ,'image/jpeg')
}
data2 = data
data2["caption"] = ""
r = requests.post(
self.bot_url + "/sendPhoto",
files=files,
data=data2,
proxies=self.getProxies(),
)
r = self.send_request(self.bot_url,"/sendPhoto",files,data2,self.getProxies())
else:
self._logger.debug(
"no image " + str(li.get("name"))
Expand Down Expand Up @@ -2218,66 +2210,41 @@ def _send_msg(
"Sending with image.. and thumbnail so image first "
+ str(chatID)
)
files = {"photo": ("image.jpg", image_data)}
files = {"photo": ("image.jpg", image_data, 'image/jpeg')}
data["caption"] = ""
r = requests.post(
self.bot_url + "/sendPhoto",
files=files,
data=data,
proxies=self.getProxies(),
)
self._logger.debug("Sending image finished. " + str(r.status_code))
r = self.send_request(self.bot_url,"/sendPhoto",files,data,self.getProxies())

self._logger.debug("Sending finished. " + str(r.status_code))

self._logger.debug("Sending thumbnail.. " + str(chatID))
files = {"photo": ("image.jpg", thumbnail_data)}
files = {"photo": ("image.jpg", thumbnail_data ,'image/jpeg')}
if message is not "":
data["caption"] = message
r = requests.post(
self.bot_url + "/sendPhoto",
files=files,
data=data,
proxies=self.getProxies(),
)
r = self.send_request(self.bot_url,"/sendPhoto",files,data,self.getProxies())
self._logger.debug("Sending finished. " + str(r.status_code))
elif image_data:
self._logger.debug("Sending with image.. " + str(chatID))
files = {"photo": ("image.jpg", image_data)}
files = {"photo": ("image.jpg", image_data ,'image/jpeg')}
# self._logger.debug("files so far: " + str(files))
if message is not "":
data["caption"] = message
r = requests.post(
self.bot_url + "/sendPhoto",
files=files,
data=data,
proxies=self.getProxies(),
)

r = self.send_request(self.bot_url,"/sendPhoto",files,data,self.getProxies())
self._logger.debug("Sending finished. " + str(r.status_code))

elif thumbnail_data != None:
self._logger.debug("Sending with thumbnail.. " + str(chatID))
files = {"photo": ("image.jpg", thumbnail_data)}
files = {"photo": ("image.jpg", thumbnail_data, 'image/jpeg')}

if message is not "":
data["caption"] = message

r = requests.post(
self.bot_url + "/sendPhoto",
files=files,
data=data,
proxies=self.getProxies(),
)

r = self.send_request(self.bot_url,"/sendPhoto",files,data,self.getProxies())
self._logger.debug("Sending finished. " + str(r.status_code))

else:
self._logger.debug("Sending without image.. " + str(chatID))
data["text"] = message
r = requests.post(
self.bot_url + "/sendMessage",
data=data,
proxies=self.getProxies(),
)
r = self.send_request(self.bot_url,"/sendMessage",None,data,self.getProxies())
self._logger.debug("Sending finished. " + str(r.status_code))

if r is not None and inline:
Expand Down Expand Up @@ -2323,6 +2290,32 @@ def _send_msg(
except Exception as ex:
self._logger.exception("Exception PostImgMethod: " + str(ex))

def send_request(self,url,urlfct,files,data,proxies):
if files != None:
r = requests.post(
url + urlfct,
files=files,
data=data,
proxies=proxies,
)
self._logger.debug("Sending image finished. " + str(r.status_code))
if r.status_code >= 300:
self._logger.error("Message with image not send because => " + str(r.reason))
self._logger.debug("full response => " + str(r.text))
message = "[ERR SEND IMAGE]\n\n " + "".join(
message
)
data["text"] = message
r = self.send_request(url,"/sendMessage",None,data,proxies)
else:
r = requests.post(
url + urlfct,
data=data,
proxies=proxies,
)
self._logger.debug("Sending message finished. " + str(r.status_code))
return r

def humanbytes(self, B):
"Return the given bytes as a human friendly KB, MB, GB, or TB string"
B = float(B)
Expand Down Expand Up @@ -2396,12 +2389,9 @@ def send_video(self, chatID, video_file, message):
return

files = {"video": open(video_file, "rb")}
r = requests.post(
self.bot_url + "/sendVideo",
files=files,
data={"chat_id": chatID, "caption": message},
proxies=self.getProxies(),
)
data={"chat_id": chatID, "caption": message}

r = self.send_request(self.bot_url,"/sendVideo",files,data,self.getProxies())
self._logger.debug(
"Sending finished. " + str(r.status_code) + " " + str(r.content)
)
Expand Down Expand Up @@ -2714,6 +2704,7 @@ def take_image(self, snapshot_url="",cam_conf=0):
)
image = Image.open(bytes_reader_class(data))
if data == None:
self._logger.debug ("data is None so return None")
return None
if flipH or flipV or rotate:

Expand All @@ -2731,8 +2722,19 @@ def take_image(self, snapshot_url="",cam_conf=0):
data = output.getvalue()
output.close()

self._logger.debug ("take_image return data : %s",str(data))
encoded_image = self.encode_image_to_base64(image)
self._logger.debug ("take_image return image as base64 : %s",str(encoded_image))
return data

def encode_image_to_base64(self, image):
"""
Encodes a PIL Image object to a base64 string for easy embedding or storage.
"""
buffered = bytes_reader_class()
image.save(buffered, format="JPEG")
return "data:image/jpeg;base64," + base64.b64encode(buffered.getvalue()).decode('utf-8')

def get_current_layers(self):
layers = None
try:
Expand Down
2 changes: 1 addition & 1 deletion octoprint_telegram/telegramCommands.py
Original file line number Diff line number Diff line change
Expand Up @@ -782,7 +782,7 @@ def cmdUpload(self, chat_id, from_id, cmd, parameter, user = ""):
)

############################################################################################
def cmdAgain(self,chat_id,from_id,cmd,parameter):
def cmdAgain(self,chat_id,from_id,cmd,parameter, user=""):
try:
data = self.main._printer.get_current_data()
if self.main._printer.is_printing():
Expand Down

0 comments on commit 0526ffe

Please sign in to comment.