telegram-bot-bash/bashbot.sh

47 lines
1.2 KiB
Bash
Raw Normal View History

2015-07-10 05:43:08 +00:00
# bashbot, the Telegram bot written in bash.
2015-11-25 03:07:39 +00:00
# Written by @topkecleon and Juan Potato (@awkward_potato)
2015-07-10 05:43:08 +00:00
# http://github.com/topkecleon/bashbot
# Depends on JSON.sh (http://github.com/dominictarr/JSON.sh),
# which is MIT/Apache-licensed.
# This file is public domain in the USA and all free countries.
# If you're in Europe, and public domain does not exist, then haha.
#!/bin/bash
2015-07-10 07:32:34 +00:00
TOKEN=''
2015-07-10 05:43:08 +00:00
URL='https://api.telegram.org/bot'$TOKEN
2015-11-25 03:07:39 +00:00
MSG_URL=$URL'/sendMessage'
PHO_URL=$URL'/sendPhoto'
2015-07-10 05:43:08 +00:00
UPD_URL=$URL'/getUpdates?offset='
OFFSET=0
2015-07-10 07:32:34 +00:00
function send_message {
2015-11-25 03:07:39 +00:00
res=$(curl "$MSG_URL" -F "chat_id=$1" -F "text=$2")
}
function send_photo() {
res=$(curl "$PHO_URL" -F "chat_id=$1" -F "photo=@$2")
2015-07-10 07:32:34 +00:00
}
2015-07-10 05:43:08 +00:00
while true; do {
2015-07-10 07:32:34 +00:00
res=$(curl $UPD_URL$OFFSET)
2015-07-10 05:43:08 +00:00
2015-07-10 07:32:34 +00:00
TARGET=$(echo $res | ./JSON.sh | egrep '\["result",0,"message","chat","id"\]' | cut -f 2)
OFFSET=$(echo $res | ./JSON.sh | egrep '\["result",0,"update_id"\]' | cut -f 2)
MESSAGE=$(echo $res | ./JSON.sh -s | egrep '\["result",0,"message","text"\]' | cut -f 2 | cut -d '"' -f 2)
2015-07-10 05:43:08 +00:00
OFFSET=$((OFFSET+1))
2015-07-10 07:32:34 +00:00
if [ $OFFSET != 1 ]; then
case $MESSAGE in
'/info') msg="This is bashbot, the Telegram bot written entirely in bash.";;
*) msg="$MESSAGE";;
esac
send_message "$TARGET" "$msg"
fi
2015-07-10 05:43:08 +00:00
} &>/dev/null; done