jansppenrade2 / Docker-Container-Updater

Automatic Docker Container Updater
The Unlicense
6 stars 2 forks source link

[Feature request] Notification to Telegram #2

Closed Keonik1 closed 5 months ago

Keonik1 commented 5 months ago

Hello again.

I would like to suggest one more feature - notifications in telegram. To get access to mail often requires a number of efforts, which is not always convenient, but to look at the notification in Telegram is very quick and easy.

I understand that I will have to almost duplicate the Send-MailNotification() function, so I will just write the nuances.

  1. The formatting can be implemented via HTML, so maybe you don't even have to rewrite anything, but use the message composing code for the mail notification.
  2. The maximum size of one message is 4096 characters (for users with and without premium subscription) and you can't send more than this limit at a time, so you may have to write a message splitter.
    {
    "ok": false,
    "error_code": 400,
    }, "description": "Bad Request: message is too long"
    }
  3. Formatting is not factored into the character count of the message, i.e. you can send a 4200 character message with formatting taken into account.
  4. Formatting guideline: https://core.telegram.org/bots/update56kabdkb12ibuisabdubodbasbdaosd
  5. Example command to send:
    
    API_TOKEN="<your_api_token>"
    CHAT_ID="<your_chat_id>"

Set the message text

MESSAGE_MD="bold *text italic *text underline ~strikethrough~ ||spoiler|| bold italic bold ~italic bold strikethrough ||italic bold strikethrough spoiler||~ underline italic bold bold "

Optional set http proxy.

export http_proxy="http://127.0.0.1:3128"

export https_proxy="http://127.0.0.1:3128"

Use the curl command to send the message

curl -s -X POST https://api.telegram.org/bot$API_TOKEN/sendMessage -d chat_id=$CHAT_ID -d parse_mode="MarkdownV2" -d text="$MESSAGE_MD"


5. If you failed to send a message due to some error, try to do it after a couple of seconds, otherwise write in the log that it failed and attach http-response.
jansppenrade2 commented 5 months ago

Hey @Keonik1,

You're keeping me busy! 😄 Thanks a lot for all the helpful information!

I like the idea, but I'll need some time to dive into the Telegram topic and test a few things.

Keonik1 commented 5 months ago

@jansppenrade2

I like the idea, but I'll need some time to dive into the Telegram topic and test a few things.

Sure, take as much time as you need to do it, it's not a life-saving feature. Thanks.

jansppenrade2 commented 5 months ago

Hi @Keonik1,

I've implemented the ability to send notifications via Telegram. However, I'm facing some challenges with splitting the message in case it exceeds the maximum length of 4096 characters. Currently, the script only checks the length of the message to be sent, and if it exceeds 4096 characters, an error will be logged, but the message will be discarded.

I'm not sure if I'll be able to implement message splitting, but you're welcome to test the script with Telegram notifications already.

Here's the link: https://github.com/jansppenrade2/Docker-Container-Updater/blob/9337451192070bed6e13ef463aa3ca60fc5b3546/container_update.sh

Please tell me what you think.

Hint: Just execute the new version of this script or add the following values manually to your config file:

[telegram]
notifications_enabled=true
retry_limit=2
retry_interval=10
chat_id=<your_chat_id>
bot_token=<your_bot_token>
Keonik1 commented 5 months ago

Hello @jansppenrade2

Thanks!

I have tested the ability to send messages to telegram. It seems to work correctly for me.

About message splitting, I'll look at how to implement it here later, because I've done something like that in the past, but probably next week.

If there are any suggestions for improvements, I'll post them in this issue or do it myself if it's easy to integrate.

jansppenrade2 commented 5 months ago

Hey @Keonik1,

I had a spontaneous idea and came up with a solution for the message splitting.

If you could test it again, that would be great. I plan to merge it into the main branch this weekend because this version also includes some bug fixes...

https://github.com/jansppenrade2/Docker-Container-Updater/blob/c694d994d25b48efba7b4e4fa6457f47d667262a/container_update.sh

Have a great weekend!

jansppenrade2 commented 5 months ago

This feature has been added in version 2024.05.26-1.