from discord_webhook import DiscordWebhook, DiscordEmbed

webhookURL = "https://discordapp.com/api/webhooks/742784012640190565/NBhtbWpDdgMZPx7uCXO8Ofw-o8Lez0V17EBGIvYv7FTIeTENK24GHNL3krUbYnOOIJ63"
maxCharPerMessage = 1994

with open ("/etc/bacula/scripts/webhooks/ChrisProEliteMail.md", "r") as myfile:
    cleaned_stdout=myfile.read()

if "Termination:            Backup OK" in cleaned_stdout:
    truncated_stdout = ["Job Backup-Chris completed with status Backup OK, omitting full report.\nAll data from nextcloud was successfully written to tape on " + datetime.now().strftime("%d/%m/%Y %H:%M:%S") + "."]
else:
    # Discord limits each message to 2000 chars, if the message is longer than that, truncate it
    truncated_stdout = [cleaned_stdout[i:i+maxCharPerMessage] for i in range(0, len(cleaned_stdout), maxCharPerMessage)] 

print (truncated_stdout) # Print the truncated message


for message_part in truncated_stdout: # For every message part in the truncated output, run this loop
    message_part = "```" + message_part + "```" # Encapsulate the messsage
    print(message_part)
    webhook = DiscordWebhook(url=webhookURL, content=message_part) # Attach the message       
    response = webhook.execute() # Hit send
