Skip to content

Commit

Permalink
webui: pre-populate telegrambot with demo commands.
Browse files Browse the repository at this point in the history
  • Loading branch information
themactep committed Nov 1, 2024
1 parent 7739239 commit 7812333
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 29 deletions.
32 changes: 32 additions & 0 deletions overlay/lower/etc/webui/telegrambot.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
telegrambot_enabled="false"
telegrambot_token=""
telegrambot_command_0="start"
telegrambot_description_0="Start conversation"
telegrambot_script_0="echo \"Hello\""
telegrambot_command_1="help"
telegrambot_description_1="Request help"
telegrambot_script_1="echo \"Try https://thingino.com/\""
telegrambot_command_2="info"
telegrambot_description_2="Information about system"
telegrambot_script_2="cat /etc/os-release"
telegrambot_command_3="diag"
telegrambot_description_3="Gather diagnostic information"
telegrambot_script_3="thingino-diag"
telegrambot_command_4="snap"
telegrambot_description_4="Take a snapshot"
telegrambot_script_4="send2telegram -i -c \$chat_id"
telegrambot_command_5=""
telegrambot_description_5=""
telegrambot_script_5=""
telegrambot_command_6=""
telegrambot_description_6=""
telegrambot_script_6=""
telegrambot_command_7=""
telegrambot_description_7=""
telegrambot_script_7=""
telegrambot_command_8=""
telegrambot_description_8=""
telegrambot_script_8=""
telegrambot_command_9=""
telegrambot_description_9=""
telegrambot_script_9=""
10 changes: 8 additions & 2 deletions overlay/lower/usr/sbin/telegrambot
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ command -v jsonfilter > /dev/null || die "$plugin is not supported on this platf
. /sbin/common-plugins
singleton

is_debug_mode && echo_c 208 "Running in debug mode"

[ "true" = "$telegrambot_enabled" ] || die "Telegram Bot is not enabled in $CONFIG_FILE."

API_URL="https://api.telegram.org/bot$telegrambot_token"
Expand Down Expand Up @@ -122,6 +124,8 @@ get_updates() {
# - my_chat_member
# - chat_member
# - chat_join_request

[ -f $OFFSET_TEMP ] || echo 0 > $OFFSET_TEMP
api_call "getUpdates" "{\"offset\":\"$(cat $OFFSET_TEMP)\",\"timeout\":$MSG_TIMEOUT,\"allowed_updates\":[\"message\",\"channel_post\"]}"
}

Expand All @@ -144,8 +148,10 @@ restore_offset
get_me
delete_my_commands
set_my_commands
get_my_commands
set_chat_menu_button
#get_my_commands
#set_chat_menu_button

trap "delete_my_commands;exit" 1 2 3 6 14 15

while :; do
sleep $((MSG_TIMEOUT / 2))
Expand Down
35 changes: 8 additions & 27 deletions package/thingino-webui/files/var/www/x/plugin-telegrambot.cgi
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ fi
<% field_switch "telegrambot_enabled" "Enable Telegram Bot" %>

<div class="input-group mb-3">
<input type="text" id="telegrambot_token" name="telegrambot_token" value="<%= $telegrambot_token %>" class="form-control" placeholder="Bot Token" aria-label="Your Telegram Bot authentication token.">
<input type="text" id="telegrambot_token" name="telegrambot_token" value="<%= $telegrambot_token %>"
class="form-control" placeholder="Bot Token" aria-label="Your Telegram Bot authentication token.">
<span class="input-group-text">
<button type="button" class="btn" data-bs-toggle="modal" data-bs-target="#helpModal">Help</button>
</span>
Expand All @@ -63,18 +64,20 @@ fi
<% for i in $(seq 0 9); do %>
<div class="row g-1 mb-3 mb-lg-1">
<div class="col-4 col-lg-2">
<input type="text" id="telegrambot_command_<%= $i %>" name="telegrambot_command_<%= $i %>" class="form-control" placeholder="Bot Command" value="<%= $(t_value "telegrambot_command_$i") %>">
<input type="text" id="telegrambot_command_<%= $i %>" name="telegrambot_command_<%= $i %>" class="form-control"
placeholder="Bot Command" value="<%= $(t_value "telegrambot_command_$i") %>">
</div>
<div class="col-8 col-lg-3">
<input type="text" id="telegrambot_description_<%= $i %>" name="telegrambot_description_<%= $i %>" class="form-control" placeholder="Command Description" value="<%= $(t_value "telegrambot_description_$i") %>">
<input type="text" id="telegrambot_description_<%= $i %>" name="telegrambot_description_<%= $i %>" class="form-control"
placeholder="Command Description" value="<%= $(t_value "telegrambot_description_$i") %>">
</div>
<div class="col-lg-7">
<input type="text" id="telegrambot_script_<%= $i %>" name="telegrambot_script_<%= $i %>" class="form-control" placeholder="Linux Command" value="<%= $(t_value "telegrambot_script_$i") %>">
<input type="text" id="telegrambot_script_<%= $i %>" name="telegrambot_script_<%= $i %>" class="form-control"
placeholder="Linux Command" value="<%= $(t_value "telegrambot_script_$i") %>">
</div>
</div>
<% done %>
</div>
<button type="button" class="btn btn-danger float-end" id="reset_commands">Reset commands</button>
<% button_submit %>
</form>
</div>
Expand Down Expand Up @@ -108,26 +111,4 @@ fi
</div>
</div>

<script>
const default_commands = [
{command:'start',script:'echo "Hello"',description:'Start conversation'},
{command:'help',script:'echo "Try https://thingino.com/"',description:'Request help'},
{command:'info',script:'cat /etc/os-release',description:'Information about system'},
{command:'diag',script:'thingino-diag',description:'Gather diagnostic information'},
{command:'snap',script:'send2telegram -i -c $chat_id',description:'Take a snapshot'},
{command:'yadisk',script:'send2yadisk && send2telegram -m "Sent to Yandex Disk" -c $chat_id',description:'Send snapshot to Yandex Disk'},
]
function resetBotCommands() {
$$('.bot-commands input[type=text]').forEach(e => e.value = '');
let i=0;
default_commands.forEach(c => {
$('#telegrambot_command_'+i).value = c.command;
$('#telegrambot_script_'+i).value = c.script;
$('#telegrambot_description_'+i).value = c.description;
i++;
});
}
$('#reset_commands').onclick = resetBotCommands;
</script>

<%in _footer.cgi %>

0 comments on commit 7812333

Please sign in to comment.