From 0f9f9aa885b32e683f5614e17f8a539a9fbf16ef Mon Sep 17 00:00:00 2001 From: celso Date: Tue, 21 Mar 2023 18:36:20 -0300 Subject: [PATCH] initial commit --- .gitmodules | 3 +++ bashbot-lib | 1 + bot.sh | 26 ++++++++++++++++++++++++++ getip.sh | 44 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 74 insertions(+) create mode 100644 .gitmodules create mode 160000 bashbot-lib create mode 100755 bot.sh create mode 100755 getip.sh diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..a619f41 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "bashbot-lib"] + path = bashbot-lib + url = milosevic@slobodan:sources/bashbot-lib.git diff --git a/bashbot-lib b/bashbot-lib new file mode 160000 index 0000000..660433d --- /dev/null +++ b/bashbot-lib @@ -0,0 +1 @@ +Subproject commit 660433de36041a28bb9e553c264d257a50c6d0f6 diff --git a/bot.sh b/bot.sh new file mode 100755 index 0000000..4b4f742 --- /dev/null +++ b/bot.sh @@ -0,0 +1,26 @@ +#!/bin/bash +[ "${1}" == "stop" ] && kill -SIGTERM "$(cat /tmp/tpdnsbot.pid)" 2>/dev/null && exit 0 +printf "%s" "${$}" > /tmp/tpdnsbot.pid + +api_url="https://api.telegram.org/bot${TELEGRAM_DNS_TOKEN}/" +botid="6040192441" +bot_tmpdir="/tmp/tpdnsbot/" +[ -d "${bot_tmpdir}" ] || { + mkdir "${bot_tmpdir}" || printf "error: can't write to /tmp\n" + exit 2 +} + +exec 2>"${bot_tmpdir}/error.log" + +. ./bashbot-lib/bashbot-lib.sh +. ./bashbot-lib/viewer.sh + +while [ "${1}" == "start" ]; do + grep "\S" <<< $(getupd) >/dev/null || continue + declare -A curmsg +# =( [msg_id]="$(getmsg_id)" [user_id]="$(getusr_id)" [chat_id]="$(getchat_id)" [text]="$(gettext)" ) + getmsg_content curmsg + view_content curmsg + [ "${curmsg[text]}" == "/start" ] && sendmsg "${curmsg[chat_id]}" $(./getip.sh "${bot_tmpdir}") + unset curmsg +done diff --git a/getip.sh b/getip.sh new file mode 100755 index 0000000..cc4fe70 --- /dev/null +++ b/getip.sh @@ -0,0 +1,44 @@ +#!/bin/bash + +get_old_ip(){ + if [ -f "${1}"ip.txt ]; then + OLD_IP=$(cat "${1}"ip.txt 2>/dev/null) + if [ -z "${OLD_IP}" ]; then + return 1 + else + return 0 + fi + else + return 1 + fi +} + +get_cur_ip(){ + CUR_IP=$(curl ifconfig.me 2>/dev/null) + if [ -z "${CUR_IP}" ]; then + return 1 + else + return 0 + fi +} + +get_old_ip "${1}" && { + get_cur_ip "${1}" && { + [ "${OLD_IP}" == "${CUR_IP}" ] && { + echo -n "${CUR_IP}" > "${1}"ip.txt + echo -n "${CUR_IP}" + exit 0 + }\ + || { + echo -n "${CUR_IP}" > "${1}"ip.txt + echo -n "${CUR_IP}" + exit 0 + } + } +}\ +|| get_cur_ip "${1}" && { + echo -n "${CUR_IP}" > "${1}"ip.txt + echo -n "${CUR_IP}" + exit 0 +} +exit "${?}"