telegram-bot-bash/bashbot.sh

129 lines
3.5 KiB
Bash
Raw Normal View History

#!/bin/bash
2015-07-10 07:43:08 +02:00
# bashbot, the Telegram bot written in bash.
2016-01-05 17:01:28 +01:00
# Written by @topkecleon, Juan Potato (@awkward_potato), Lorenzo Santina (BigNerd95) and Daniil Gentili (danog)
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
send_message() {
2016-01-05 17:01:28 +01:00
local chat="$1"
local text="$(echo "$2" | sed 's/ mykeyboardstartshere.*//g')"
local keyboard="$(echo "$2" | sed '/mykeyboardstartshere /!d;s/.*mykeyboardstartshere //g')"
if [ "$keyboard" = "" ]; then
res=$(curl -s "$MSG_URL" -F "chat_id=$chat" -F "text=$text")
else
send_keyboard "$chat" "$text" "$keyboard"
fi
2016-01-03 02:14:07 +01:00
}
2016-01-05 17:01:28 +01:00
2016-01-03 02:14:07 +01:00
send_keyboard() {
2016-01-05 17:01:28 +01:00
local chat="$1"
local text="$2"
shift 2
keyboard=init
for f in $*;do keyboard="$keyboard, [\"$f\"]";done
keyboard=${keyboard/init, /}
res=$(curl -s "$MSG_URL" --header "content-type: multipart/form-data" -F "chat_id=$chat" -F "text=$text" -F "reply_markup={\"keyboard\": [$keyboard],\"one_time_keyboard\": true}")
2015-11-25 04:07:39 +01:00
}
send_photo() {
2016-01-05 17:01:28 +01:00
res=$(curl -s "$PHO_URL" -F "chat_id=$1" -F "photo=@$2")
2015-07-10 09:32:34 +02:00
}
2016-01-05 17:01:28 +01:00
startproc() {
local copname="$1"
local TARGET="$2"
mkdir -p "$copname"
mkfifo $copname/out
tmux new-session -d -n $copname "./question $TARGET 2>&1>$copname/out"
local pid=$(ps aux | sed '/tmux/!d;/'$copname'/!d;/sed/d;s/'$USER'\s*//g;s/\s.*//g')
echo $pid>$copname/pid
while ps aux | grep -v grep | grep -q $pid;do
read -t 10 line
[ "$line" != "" ] && send_message "$TARGET" "$line"
line=
done <$copname/out
2016-01-02 21:51:45 +01:00
}
2016-01-03 02:14:07 +01:00
inproc() {
2016-01-05 17:01:28 +01:00
local copname="$1"
local copid="$2"
shift 2
tmux send-keys -t $copname "$@
"
ps aux | grep -v grep | grep -q "$copid" || { rm -r $copname; };
2016-01-03 02:14:07 +01:00
}
process_client() {
local MESSAGE=$1
local TARGET=$2
local msg=""
2016-01-05 17:01:28 +01:00
local copname="CO$TARGET"
local copidname="$copname/pid"
local copid="$(cat $copidname 2>/dev/null)"
if [ "$copid" = "" ]; then
2016-01-02 21:51:45 +01:00
case $MESSAGE in
2016-01-05 17:01:28 +01:00
'/question')
startproc "$copname" "$TARGET"&
;;
'/info')
send_message "$TARGET" "This is bashbot, the Telegram bot written entirely in bash."
;;
2016-01-05 17:05:26 +01:00
'/start')
send_message "$TARGET" "This is bashbot, the Telegram bot written entirely in bash.
Features background tasks and interactive chats.
Can serve as an interface for cli programs.
Currently can send messages, custom keyboards and photos.
2016-01-05 17:14:47 +01:00
Available commands:
/start: Start bot and get this message.
/info: Get shorter info message about this bot.
/question: Start interactive chat.
/cancel: Cancel any currently running interactive chats.
2016-01-05 17:05:26 +01:00
Written by @topkecleon, Juan Potato (@awkward_potato), Lorenzo Santina (BigNerd95) and Daniil Gentili (danog)
http://github.com/topkecleon/bashbot
"
;;
2016-01-05 17:01:28 +01:00
*)
send_message "$TARGET" "$MESSAGE"
2016-01-02 21:51:45 +01:00
esac
2016-01-05 17:01:28 +01:00
else
2016-01-02 21:51:45 +01:00
case $MESSAGE in
2016-01-05 17:01:28 +01:00
'/cancel')
kill $copid
rm -r $copname
send_message "$TARGET" "Command canceled."
;;
*) inproc "$copname" "$copid" "$MESSAGE";;
2016-01-02 21:51:45 +01:00
esac
2016-01-05 17:01:28 +01:00
fi
}
2015-07-10 07:43:08 +02:00
while true; do {
2016-01-05 17:01:28 +01:00
res=$(curl -s $UPD_URL$OFFSET)
2015-07-10 07:43:08 +02:00
2016-01-05 17:01:28 +01: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"&
2015-07-10 09:32:34 +02:00
fi
2015-07-10 07:43:08 +02:00
2016-01-05 17:01:28 +01:00
}; done