Skip to content

Instantly share code, notes, and snippets.

@sayan1999
Last active February 8, 2021 07:31
Show Gist options
  • Save sayan1999/b9d976a71d4aa0d3578fe9fa8a9dabff to your computer and use it in GitHub Desktop.
Save sayan1999/b9d976a71d4aa0d3578fe9fa8a9dabff to your computer and use it in GitHub Desktop.
Linux Command Completion Status Notifier on Discord Channel
# put the snippet in your .bashrc or .zshrc (or the shell you prefer to use)
# and call it like ```notifire <command>```
# ```notifire sleep 5```
notifire()
{
"$@"
ret=$?
if [[ $ret -eq 0 ]]
then
# "~/.custom-commands/lib/Notifire" is the path where I kept the webhook python script
python3 ~/.custom-commands/lib/Notifire/discord_notifier.py $(uname) $(hostname) Successfully ran $@
else
python3 ~/.custom-commands/lib/Notifire/discord_notifier.py $(uname) $(hostname) Failed $@
fi
}
  1. Create a dedicated discord server, create a webhook to the server and copy the webhook link.
  2. Replace in discord_notifier.py with copied webhook link and save the file to a location.
  3. Copy the location and replace ~/.custom-commands/lib/Notifire with your location in the .bashrc file in this gist.
  4. Now copy paste the modified .bashrc content in this gist to your terminal startup script.
  5. Run any command followed by notifier and get notification on discord. (for ex: notifire sleep 2)
  6. Video guide here
# this is the python script to notify via discord channel
import requests, sys
url = "<discord webhook url here>"
data = {
}
data["embeds"] = [
{
"description" : f"{' '.join(sys.argv[1:])}",
"title" : f"Notification from {' '.join(sys.argv[1:3])}"
}
]
result = requests.post(url, json = data)
try:
result.raise_for_status()
except requests.exceptions.HTTPError as err:
print('NotiFire Err', err)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment