Instantly share code, notes, and snippets.

Embed
What would you like to do?
Deployment Two-Factory Telegram Auth
#! /bin/bash
API=https://api.telegram.org/${MY_BOT_ID}
CHAT_ID=${CHAT_ID_FOR_THE_CHAT_BETWEEN_MY_BOT_AND_ME}
lastId=`curl -s $API/getUpdates | jq '.result[0].update_id'`
offset=$((lastId+1))
curl -s -X POST -H 'Content-Type: application/json' -d \
"{
\"chat_id\": ${CHAT_ID},
\"text\": \"Hi Tim, would you like to accept a connection from Travis CI?\n(Command: $SSH_ORIGINAL_COMMAND)\",
\"reply_markup\": {
\"one_time_keyboard\": true,
\"resize_keyboard\": true,
\"keyboard\": [
[ { \"text\": \"Yes, please!\" } ],
[ { \"text\": \"No!\" } ]
]
}
}" $API/sendMessage &> /dev/null
# Wait for incoming message
incoming=`curl -s "$API/getUpdates?offset=$offset&timeout=40"`
# Extract username and sent text from response
username="$(echo $incoming | jq -r '.result[0].message.from.username')"
text="$(echo $incoming | jq -r '.result[0].message.text')"
if [[ $SSH_ORIGINAL_COMMAND == rsync* ]] && [ "$username" == "timroes" ] && [[ "${text,,}" == yes* ]]; then
curl -s -X POST -d "chat_id=${CHAT_ID}&text=OK. Travis will now update files." $API/sendMessage &> /dev/null
$SSH_ORIGINAL_COMMAND
else
curl -s -X POST -d "chat_id=${CHAT_ID}&text=Since I didn't get an OK from you, I won't allow Travis to deploy." $API/sendMessage &> /dev/null
exit
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment