Skip to content

Instantly share code, notes, and snippets.

@davidlj95
Created July 21, 2017 05:37
Show Gist options
  • Save davidlj95/eb5be02dc19ff34df04163b629a1be3a to your computer and use it in GitHub Desktop.
Save davidlj95/eb5be02dc19ff34df04163b629a1be3a to your computer and use it in GitHub Desktop.
Allows to send a message to a Telegram contact once configured telegram-cli (vysheng/tg) without entering the telegram-cli and using the contact / group name, not its ide
#!/bin/bash
# Uses telegram-cli to send a message to the given destination
# Usage: ./telegram_msg <DESTINATION> <MESSAGE>
#==============================================================
# Constants
#==============================================================
APP_NAME=telegram_msg
# Name of the app as appears in logs
APP="/usr/local/bin/telegram-cli"
# telegram-cli location
PUBKEY="/opt/telegram-cli/tg-server.pub"
INIT_TIME=0.5
# Time telegram-cli bootstraps including retrieving
# contacts
#==============================================================
# Arguments
#==============================================================
DESTINATION=$1
MESSAGE=$2
shift
#==============================================================
# Code
#==============================================================
#--------------------------------------------------------------
# 1. Check arguments
#--------------------------------------------------------------
# Destination
if [[ -z "$DESTINATION" ]]; then
>&2 echo "Destination not set"
logger -t $APP_NAME "Failed to send: Destination unset"
exit 1
fi
# Message
if [[ -z "$MESSAGE" ]]; then
>&2 echo "Message not set"
logger -t $APP_NAME "Failed to send: Message unset"
exit 2
fi
# Join messages
# # Make temporal file with all messages
tmp_file=$(mktemp)
while (( "$#" )); do
echo "$1" >> $tmp_file
shift
done
#--------------------------------------------------------------
# 2. Send
#--------------------------------------------------------------
((sleep $INIT_TIME && echo send_text $DESTINATION $tmp_file) | \
$APP -k $PUBKEY -W) # &> /dev/null
# Log
logger -t $APP_NAME "Sent message via Telegram @ bitcoin"
# Delete temporaries and finish
rm $tmp_file
echo "$USER sent a message via Telegram"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment