|
|
@@ -11,26 +11,15 @@ from email.mime.image import MIMEImage
|
|
|
from email.mime.multipart import MIMEMultipart
|
|
|
from email.mime.text import MIMEText
|
|
|
|
|
|
-
|
|
|
def send_to_telegram(bot_token, chat_id, caption, *image_paths):
|
|
|
url = f"https://api.telegram.org/bot{bot_token}/sendMediaGroup"
|
|
|
|
|
|
media_group = []
|
|
|
|
|
|
for i, image_path in enumerate(image_paths):
|
|
|
- with open(image_path, 'rb') as image_file:
|
|
|
- image_data = image_file.read()
|
|
|
-
|
|
|
- files = {
|
|
|
- 'photo': ('image.jpg', image_data, 'image/jpeg'),
|
|
|
- }
|
|
|
-
|
|
|
- response = requests.post(f"https://api.telegram.org/bot{bot_token}/uploadPhoto", files=files)
|
|
|
- file_id = response.json()['result']['photo'][0]['file_id']
|
|
|
-
|
|
|
media_group.append({
|
|
|
'type': 'photo',
|
|
|
- 'media': file_id,
|
|
|
+ 'media': 'attach://image' + str(i),
|
|
|
'caption': caption if i == 0 else "",
|
|
|
'parse_mode': 'HTML',
|
|
|
})
|
|
|
@@ -40,12 +29,15 @@ def send_to_telegram(bot_token, chat_id, caption, *image_paths):
|
|
|
'media': json.dumps(media_group),
|
|
|
}
|
|
|
|
|
|
- response = requests.post(url, data=data)
|
|
|
+ files = {
|
|
|
+ f'image{i}': (f'image{i}.jpg', open(image_path, 'rb')) for i, image_path in enumerate(image_paths)
|
|
|
+ }
|
|
|
+
|
|
|
+ response = requests.post(url, data=data, files=files)
|
|
|
|
|
|
if response.status_code != 200:
|
|
|
raise ValueError(f"Request to Telegram API returned an error: {response.status_code}, {response.text}")
|
|
|
|
|
|
-
|
|
|
def create_email(sender_email, recipient_emails, subject, boot_time_str, image_path1, image_path2):
|
|
|
msg = MIMEMultipart("related")
|
|
|
msg["From"] = sender_email
|
|
|
@@ -119,4 +111,4 @@ try:
|
|
|
except ValueError as e:
|
|
|
print(f"Failed to send images to Telegram: {e}")
|
|
|
|
|
|
-tmpdir.cleanup()
|
|
|
+tmpdir.cleanup()
|