Posted 15 March, 2017
We recently moved over from Slack to a self-hosted communication app : Rocket.chat . One of the things I really enjoyed on slack, was the fact that there is a huge amount of integrations. Even from bash one can write to slack. This is particularly nice for reports of cron jobs, previously I relied on e-mails, but those tend to get ignored/spammed/… not read. So I let cron jobs also slack me. On Rocket.chat there are also incoming hooks, but I could not find a script that made it dummy prove to use like the one from Sul Aga so I took his code and adapted it to work with Rocket.chat. Here is how you can do the same.
under administration –> integrations : add new incoming webhook
class Script { process_incoming_request({ request }) { console.log(request.content); return { content:{ text: request.content.text } }; return { error: { success: false, message: 'Error example' } }; } }
The only thing you need now is the Webhook URL.
Based on this script.
#!/usr/bin/env bash function usage { programName=$0 echo "description: use this program to post messages to Rocket.chat channel" echo "usage: $programName [-b \"message body\"] [-u \"rocket.chat url\"]" echo " -b The message body" echo " -u The rocket.chat hook url to post to" exit 1 } while getopts ":b:u:h" opt; do case ${opt} in u) rocketUrl="$OPTARG" ;; b) msgBody="$OPTARG" ;; h) usage ;; \?) echo "Invalid option -$OPTARG" >&2 ;; esac done if [[ ! "${rocketUrl}" || ! "${msgBody}" ]]; then echo "all arguments are required" usage fi read -d '' payLoad << EOF {"text": "${msgBody}"} EOF echo $payLoad statusCode=$(curl \ --write-out %{http_code} \ --silent \ --output /dev/null \ -X POST \ -H 'Content-type: application/json' \ --data "${payLoad}" ${rocketUrl}) echo ${statusCode}
Now to use it is simple; Take webhook url, and start posting messages.
./postToRocket.sh -b "everything is fine today" -u ROCKET_CHAT_URL
And that’s it folks ! Happy spamming Rocket.chat 🙂
If you enjoyed this article, please consider buying me a Dr Pepper.
Fuel the beast!
Buy me a Dr Pepper
This is exactly what I was looking for to send notifications from scheduled scripts. Worked perfectly. Thanks.
You’re welcome 🙂
Seems I cannot use @all in the message text, any idea why? Get a 400 error then.
No idea, sorry ! Would guess some sort of protection ?
This is saving my life
Thanks a lot
This is great! Thank you:-)
I’m having issues, it seems it arrives a blank message and a warning appears on the output of the script
line 37: warning: here-document at line 26 delimited by end-of-file (wanted `EOF’)
I think it could be a quotes issue?
nevermind, I found the problem:
When copypasting the code, it finished indented on vim, and the second EOF must be unindented.
By removing the indentation, everything works perfect!
Thank you, now I can send my BURP backup notifications directly to RocketChat! 😀
[…] Pour passer du texte depuis mes machines, en BASH, je me suis inspiré de ceci. […]