Skip to content

Instantly share code, notes, and snippets.

@dominik-hadl
Created January 21, 2025 13:33
Show Gist options
  • Save dominik-hadl/4b31fa05a104320b8bb480a529ca93b2 to your computer and use it in GitHub Desktop.
Save dominik-hadl/4b31fa05a104320b8bb480a529ca93b2 to your computer and use it in GitHub Desktop.
# RouterOS script to forward all incoming sms messages to Slack
# Run this script as a scheduled task e.g. every 10 minutes
# Author: Dominik Hadl
# Date: 2024-12-01
# Version: 1.0
# Platform: RouterOS 7.15.3 (Mikrotik Chateau LTE6)
# setting variables
:local slackHookUrl "REPLACE_YOUR_SLACK_HOOK_URL";
:local cHeader "Content-Type: application/json";
:log info "Forwarding all received SMS to Slack";
:foreach sms in [/tool sms inbox find] do={
# retrieve the sender, message body and timestamp from the $inboxData array
# as the sms inbox messages-array start with index "0" we need to substract 1 from the index value
:local sender [/tool sms inbox get $sms value-name=phone]
:local timestamp [/tool sms inbox get $sms value-name=timestamp]
:local message [/tool sms inbox get $sms value-name=message]
:do {
:local notification ">*$timestamp* from *$sender*\\n>$message";
:local pload "{\"text\": \"$notification\"}";
:log info "Payload to Slack: $pload";
# send the message to the slack channel
/tool fetch http-method=post http-header-field=$cHeader http-data=$pload url=$slackHookUrl;
# Remove the message if it was sent succesfully
/tool sms inbox remove $sms;
} on-error={
:log error "Forwarding SMS ($index) to Slack failed"
# send a message to slack that the SMS could not be sent
:local errortext "{\"text\":\"Failed to forward SMS from Mikrotik to Slack.\"}";
/tool fetch http-method=post http-header-field=$cHeader http-data=errortext url=$slackHookUrl;
}
}
:log info "Finished forwarding SMS to Slack."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment