2016-01-02 20:35:15 +01:00
|
|
|
#!/bin/bash
|
2015-07-10 07:43:08 +02:00
|
|
|
# bashbot, the Telegram bot written in bash.
|
2015-12-26 02:58:54 +01:00
|
|
|
# Written by @topkecleon, Juan Potato (@awkward_potato) and Lorenzo Santina (BigNerd95)
|
2015-07-10 07:43:08 +02: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.
|
|
|
|
|
|
|
|
|
2015-07-10 09:32:34 +02:00
|
|
|
TOKEN=''
|
2015-07-10 07:43:08 +02:00
|
|
|
URL='https://api.telegram.org/bot'$TOKEN
|
2015-11-25 04:07:39 +01:00
|
|
|
MSG_URL=$URL'/sendMessage'
|
|
|
|
PHO_URL=$URL'/sendPhoto'
|
2015-07-10 07:43:08 +02:00
|
|
|
UPD_URL=$URL'/getUpdates?offset='
|
|
|
|
OFFSET=0
|
|
|
|
|
2015-12-26 02:47:10 +01:00
|
|
|
send_message() {
|
2015-11-25 04:07:39 +01:00
|
|
|
res=$(curl "$MSG_URL" -F "chat_id=$1" -F "text=$2")
|
|
|
|
}
|
|
|
|
|
2015-12-26 02:47:10 +01:00
|
|
|
send_photo() {
|
2015-11-25 04:07:39 +01:00
|
|
|
res=$(curl "$PHO_URL" -F "chat_id=$1" -F "photo=@$2")
|
2015-07-10 09:32:34 +02:00
|
|
|
}
|
|
|
|
|
2016-01-02 20:35:15 +01:00
|
|
|
readproc() {
|
|
|
|
coproc="coproc$1"
|
2016-01-02 20:46:34 +01:00
|
|
|
while true;do read msg <&${coproc["0"]}; [ "$?" != "0" ] && return || send_message "$TARGET" "$msg";done
|
2016-01-02 20:35:15 +01:00
|
|
|
}
|
2015-12-26 02:47:10 +01:00
|
|
|
process_client() {
|
|
|
|
local MESSAGE=$1
|
|
|
|
local TARGET=$2
|
|
|
|
local msg=""
|
|
|
|
case $MESSAGE in
|
|
|
|
'/info') msg="This is bashbot, the Telegram bot written entirely in bash.";;
|
2016-01-02 20:46:34 +01:00
|
|
|
'/question') coproc "coproc$TARGET" { question; }; readproc $TARGET; return;
|
2015-12-26 02:47:10 +01:00
|
|
|
*) msg="$MESSAGE";;
|
|
|
|
esac
|
2016-01-02 20:35:15 +01:00
|
|
|
send_message "$TARGET" "$msg"&
|
2015-12-26 02:47:10 +01:00
|
|
|
}
|
|
|
|
|
2015-07-10 07:43:08 +02:00
|
|
|
while true; do {
|
|
|
|
|
2015-07-10 09:32:34 +02:00
|
|
|
res=$(curl $UPD_URL$OFFSET)
|
2015-07-10 07:43:08 +02:00
|
|
|
|
2015-07-10 09:32:34 +02: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 07:43:08 +02:00
|
|
|
|
|
|
|
OFFSET=$((OFFSET+1))
|
|
|
|
|
2015-07-10 09:32:34 +02:00
|
|
|
if [ $OFFSET != 1 ]; then
|
2016-01-02 20:46:34 +01:00
|
|
|
process_client "$MESSAGE" "$TARGET"&
|
2016-01-02 20:35:15 +01:00
|
|
|
|
2015-07-10 09:32:34 +02:00
|
|
|
fi
|
2015-07-10 07:43:08 +02:00
|
|
|
|
|
|
|
} &>/dev/null; done
|