This repository was archived by the owner on Jul 26, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimplebot.sh
More file actions
executable file
·150 lines (102 loc) · 4.39 KB
/
simplebot.sh
File metadata and controls
executable file
·150 lines (102 loc) · 4.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/bin/bash
# Very simple Telegram bot using bashegram
# edit to use your actual value here
tg_get_userdef_credentials(){
tg_lib['bot_token']="111111111zzzzzzzzzzzzzzzzzzzzzxxxxxxxxxxxxxxxxxx"
}
########################## callback functions ####################
# process a "message" update
process_message(){
local update=$1
local -A message_obj update_obj
tg_parse_object update_obj "$update"
tg_parse_object message_obj "${update_obj[message]}"
# now message_obj contains the message
local message_id=${message_obj[message_id]}
local text=${message_obj[text]}
local chat_id=${message_obj[chat.id]}
local sender=${message_obj[from.first_name]}
# check whether it's a command
if [[ "$text" =~ ^/ ]]; then
# remove bot name at the end, if present
text=${text%@"${tg_lib['bot_username']}"}
case "$text" in
/start)
tg_do_request sendMessage "chat_id=${chat_id}" "text=Hello $sender, this is ${tg_lib['bot_name']}"
;;
/help)
tg_do_request sendMessage "chat_id=${chat_id}" "text=Following commands are available: '/start', '/echo <text>', '/getphoto <path>', '/keyboard'"
;;
/settings)
tg_do_request sendMessage "chat_id=${chat_id}" "text='$text' not implemented"
;;
/echo\ *)
# echo the message back
local msg="${text#/echo }"
tg_do_request sendMessage "chat_id=${chat_id}" "text=$msg"
;;
/keyboard)
# send a custom keyboard until user says stop
local keyboard=$(tg_create_reply_keyboard false true "/samplechoice1" '|' "/samplechoice2" '|' "/stop")
tg_do_request sendMessage "chat_id=${chat_id}" "text=Sending reply keyboard" "reply_markup=${keyboard}"
;;
/samplechoice1|/samplechoice2)
tg_do_request sendMessage "chat_id=${chat_id}" "text=You pressed the '$text' key"
;;
/stop)
tg_do_request sendMessage "chat_id=${chat_id}" "text=Removing reply keyboard" "reply_markup={\"remove_keyboard\":true}"
;;
/getphoto\ *)
# THIS ACCESSES LOCAL FILES, FOR TESTING ONLY!!!!
local photo="${text#/getphoto }"
if [ -f "$photo" ]; then
# note the @ before the filename, so the library knows it's a local file
tg_do_request sendPhoto "chat_id=${chat_id}" "photo=@${photo}" "caption=${photo}"
else
tg_do_request sendMessage "chat_id=${chat_id}" "text=Photo '$photo' not found"
fi
;;
*)
tg_do_request sendMessage "chat_id=${chat_id}" "text='$text' not implemented"
;;
esac
fi
}
# process an "inline_query" update
process_inline_query(){
local update=$1
local -A inline_query_obj update_obj
tg_parse_object update_obj "$update"
tg_parse_object inline_query_obj "${update_obj[inline_query]}"
# now inline_query_obj has the message
local from_id=${inline_query_obj[from.id]}
local query=${inline_query_obj[query]}
local inline_query_id=${inline_query_obj[id]}
# what we do here is just send back an inline_query_reply of type "article" with the
# same query we received, but of course the real thing can and should be more useful
if [ "$query" = "" ]; then
return
fi
# this has to be an array of results, we use the "InlineQueryResultArticle" here
declare -A inline_query_reply=()
inline_query_reply['0.type']="article"
inline_query_reply['0.id']="$RANDOM"
inline_query_reply['0.title']="$query"
inline_query_reply['0.input_message_content.message_text']="$query"
result_json=$(tg_create_object inline_query_reply)
tg_do_request answerInlineQuery "inline_query_id=${inline_query_id}" "results=${result_json}"
}
######### BEGIN #################
. bashegram.sh || exit 1
#tg_set_log_level DEBUG # optional: raise log level
tg_api_init || { tg_log ERROR "Cannot initialize bot API, terminating" && exit 1; }
#tg_set_long_polling_timeout 30 # optional: set long polling timeout (default and maximum: 50 seconds)
tg_set_callback "message" process_message || { tg_log ERROR "Failed to set callback for 'message', terminating" && exit 1; }
tg_set_callback "inline_query" process_inline_query || { tg_log ERROR "Failed to set callback for 'inline_query', terminating" && exit 1; }
# only receive message we can handle
tg_update_filter_disallow "*"
tg_update_filter_allow message inline_query
# optional: filter by user
#tg_sender_disallow "*"
#tg_sender_allow 123456 @johndoe
tg_bot_main_loop