forked from whatwewant/chatgpt-for-chatbot-feishu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
entrypoint.sh
executable file
·125 lines (96 loc) · 3.25 KB
/
entrypoint.sh
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
#!/bin/zmicro
help() {
echo "Usage:"
echo " chatgpt-for-chatbot-feishu"
}
core() {
if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
help
exit 0
fi
dotenv::try_load
local PORT=${PORT:-8080}
local API_PATH=${API_PATH:-"/"}
log::info "[$(timestamp)] run chatgpt for chatbot feishu with zmicro ..."
if [ "$TUNNEL_ENABLE" = "true" ]; then
runtunnel
fi
log::info "[$(timestamp)] starting chatgpt for chatbot feishu ..."
chatgpt-for-chatbot-feishu
}
runtunnel() {
local tunnel_type="$TUNNEL_TYPE"
local tunnel_auth_token="$TUNNEL_AUTH_TOKEN"
local tunnel_subdomain="$TUNNEL_SUBDOMAIN"
local tunnel_log=$(os::tmp_file)
echo
log::info "[$(timestamp)] enable tunnel $tunnel_type (logfile: $tunnel_log)..."
# TUNNEL NGROK
if [ "$tunnel_type" = "ngrok" ]; then
if [ -n "$tunnel_subdomain" ] && [ -z "$tunnel_auth_token" ]; then
log::error "[$(timestamp)] tunnel_auth_token is required when use tunnel_subdomain"
return 1
fi
if [ -n "$tunnel_auth_token" ]; then
zmicro ngrok config add-authtoken $tunnel_auth_token >>/dev/null
fi
if [ -n "$tunnel_subdomain" ]; then
zmicro ngrok http --subdomain "$tunnel_subdomain" ${PORT} --log $tunnel_log >>$tunnel_log 2>&1 &
else
zmicro ngrok http ${PORT} --log $tunnel_log >>$tunnel_log 2>&1 &
fi
log::info "[$(timestamp)] starting ngrok ..."
# sleep 3
local url=""
while [ -z "$url" ]; do
sleep 1
log::info "[$(timestamp)] checking whether ngrok connected ..."
url=$(cat $tunnel_log | grep "url=" | head -n 1 | awk -F '=' '{print $8}')
if [ -n "$url" ]; then
break
fi
if [ "$DEBUG" = "true" ]; then
log::info "[$(timestamp)] show ngrok connection info start ..."
cat $tunnel_log
log::info "[$(timestamp)] show ngrok connection info end ..."
fi
done
log::info "[$(timestamp)] ngrok url: $(color::green $url)"
export SITE_URL=$url
elif [ "$TUNNEL_TYPE" = "cpolar" ]; then
if [ -n "$tunnel_subdomain" ] && [ -z "$tunnel_auth_token" ]; then
log::error "[$(timestamp)] tunnel_auth_token is required when use tunnel_subdomain"
return 1
fi
if [ -n "$tunnel_auth_token" ]; then
zmicro cpolar authtoken $tunnel_auth_token >>/dev/null
fi
if [ -n "$tunnel_subdomain" ]; then
zmicro cpolar http --subdomain "$tunnel_subdomain" ${PORT} --log $tunnel_log >>$tunnel_log 2>&1 &
else
zmicro cpolar http ${PORT} --log $tunnel_log >>$tunnel_log 2>&1 &
fi
log::info "[$(timestamp)] starting cpolar ..."
# sleep 3
local cpolar_url=""
while [ -z "$cpolar_url" ]; do
sleep 1
log::info "[$(timestamp)] checking whether cpolar connected ..."
cpolar_url=$(cat $tunnel_log | grep "established" | grep "https" | awk -F 'at ' '{print $2}' | awk -F '"' '{print $1}')
if [ -n "$cpolar_url" ]; then
break
fi
if [ "$DEBUG" = "true" ]; then
log::info "[$(timestamp)] show cpolar connection info start ..."
cat $tunnel_log
log::info "[$(timestamp)] show cpolar connection info end ..."
fi
done
log::info "[$(timestamp)] cpolar url: $(color::green $cpolar_url)"
export SITE_URL=$cpolar_url
fi
}
run() {
core "$@"
}
run "$@"