1 | #!/usr/bin/env bash
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 | set -e
|
8 |
|
9 | export HOME=/bot
|
10 | export PATH=$PATH:/wechaty/bin:/wechaty/node_modules/.bin
|
11 |
|
12 | function wechaty::banner() {
|
13 | echo
|
14 | figlet " Wechaty "
|
15 | echo ____________________________________________________
|
16 | echo " https://www.chatie.io"
|
17 | }
|
18 |
|
19 | function wechaty::errorBotNotFound() {
|
20 | local file=$1
|
21 | echo "Container ERROR: can not found bot file: $HOME/$file"
|
22 |
|
23 | echo "Container PWD: $(pwd)"
|
24 | echo "Container HOME: $HOME"
|
25 | echo "Container LS $HOME: $(ls -l $HOME)"
|
26 |
|
27 | figlet " Troubleshooting "
|
28 | cat <<'TROUBLESHOOTING'
|
29 |
|
30 | Troubleshooting:
|
31 |
|
32 | 1. Did you bind the current directory into container?
|
33 |
|
34 | check your `docker run ...` command, if there's no `volumn` arg,
|
35 | then you need to add it so that we can bind the volume of /bot:
|
36 |
|
37 | `--volume="$(pwd)":/bot`
|
38 |
|
39 | this will let the container visit your current directory.
|
40 |
|
41 | 2. Are you sure your .js/.ts files aren't .js.txt/.ts.txt?
|
42 |
|
43 | this could be a problem on new Windows installs (file
|
44 | extensions hidden by default).
|
45 |
|
46 | if you still have issue, please have a look at
|
47 | https://github.com/wechaty/wechaty/issues/66
|
48 | and do a search in issues, that might be help.
|
49 |
|
50 | TROUBLESHOOTING
|
51 | }
|
52 |
|
53 | function wechaty::printEnv () {
|
54 | num=$(env | grep -c WECHATY)
|
55 | echo "WECHATY Environment Variables: $num"
|
56 | env | grep WECHATY
|
57 | }
|
58 |
|
59 | function wechaty::errorCtrlC () {
|
60 |
|
61 |
|
62 | echo ' Script terminated by Control-C '
|
63 | figlet ' Ctrl + C '
|
64 | }
|
65 |
|
66 | function wechaty::pressEnterToContinue() {
|
67 | local -i timeoutSecond=${1:-30}
|
68 | local message=${2:-'Press ENTER to continue ... '}
|
69 |
|
70 | read -r -t "$timeoutSecond" -p "$message" || true
|
71 | echo
|
72 | }
|
73 |
|
74 | function wechaty::diagnose() {
|
75 | local -i ret=$1 && shift
|
76 | local file=$1 && shift
|
77 |
|
78 | echo "ERROR: Bot exited with code $ret"
|
79 |
|
80 | figlet ' BUG REPORT '
|
81 | wechaty::pressEnterToContinue 30
|
82 |
|
83 | echo
|
84 | echo "### 1. source code of $file"
|
85 | echo
|
86 | cat "$HOME/$file" || echo "ERROR: file not found"
|
87 | echo
|
88 |
|
89 | echo
|
90 | echo "### 2. directory structor of $HOME"
|
91 | echo
|
92 | ls -l "$HOME"
|
93 |
|
94 | echo
|
95 | echo '### 3. package.json'
|
96 | echo
|
97 | cat "$HOME"/package.json || echo "No package.json"
|
98 |
|
99 | echo
|
100 | echo "### 4. directory structor inside $HOME/node_modules"
|
101 | echo
|
102 | ls "$HOME"/node_modules || echo "No node_modules"
|
103 |
|
104 | echo
|
105 | echo '### 5. wechaty doctor'
|
106 | echo
|
107 | wechaty-doctor
|
108 |
|
109 | figlet " Submit a ISSUE "
|
110 | echo _____________________________________________________________
|
111 | echo '####### please paste all the above diagnose messages #######'
|
112 | echo
|
113 | echo 'Wechaty Issue https://github.com/wechaty/wechaty/issues'
|
114 | echo
|
115 |
|
116 | wechaty::pressEnterToContinue
|
117 | }
|
118 |
|
119 | function wechaty::runBot() {
|
120 | local botFile=$1
|
121 |
|
122 | if [ ! -f "$HOME/$botFile" ]; then
|
123 | wechaty::errorBotNotFound "$botFile"
|
124 | return 1
|
125 | fi
|
126 |
|
127 | echo "Working directory: $HOME"
|
128 | cd "$HOME"
|
129 |
|
130 | [ -f package.json ] && {
|
131 |
|
132 |
|
133 |
|
134 |
|
135 |
|
136 |
|
137 | echo "Please make sure you had installed all the NPM modules which is depended on your bot script."
|
138 |
|
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 |
|
145 | }
|
146 |
|
147 |
|
148 |
|
149 |
|
150 |
|
151 |
|
152 |
|
153 | local -i ret=0
|
154 | case "$botFile" in
|
155 | *.js)
|
156 | if [ "$NODE_ENV" = "production" ]; then
|
157 | echo "Executing node $*"
|
158 | node "$@" &
|
159 | else
|
160 | echo "Executing babel-node --presets @babel/env $*"
|
161 |
|
162 | BABEL_DISABLE_CACHE=1 babel-node --presets @babel/env "$@" &
|
163 | fi
|
164 | ;;
|
165 | *.ts)
|
166 | if [ "$NODE_ENV" = "production" ]; then
|
167 | echo "Executing ts-node $*"
|
168 | ts-node "$@" &
|
169 | else
|
170 | echo "Executing ts-node --type-check $*"
|
171 | ts-node --type-check "$@" &
|
172 | fi
|
173 | ;;
|
174 | *)
|
175 | echo "ERROR: wechaty::runBot() neith .js nor .ts"
|
176 | exit 1 &
|
177 | esac
|
178 |
|
179 | wait "$!" || ret=$?
|
180 |
|
181 | case "$ret" in
|
182 | 0)
|
183 | ;;
|
184 | 130)
|
185 | wechaty::errorCtrlC
|
186 | ;;
|
187 | *)
|
188 | wechaty::diagnose "$ret" "$@"
|
189 | ;;
|
190 | esac
|
191 |
|
192 | return "$ret"
|
193 | }
|
194 |
|
195 |
|
196 |
|
197 | function wechaty::linkBotNodeModules() {
|
198 | for localModule in /bot/node_modules/*; do
|
199 | [ -e "$localModule" ] || continue
|
200 |
|
201 | module=${localModule//\/bot\/node_modules\//}
|
202 |
|
203 | globalModule="/node_modules/$module"
|
204 |
|
205 | if [ ! -e "$globalModule" ]; then
|
206 | ln -sf "$localModule" /node_modules/
|
207 |
|
208 |
|
209 | fi
|
210 | done
|
211 | }
|
212 |
|
213 | function wechaty::io-client() {
|
214 | figlet " Chatie.io "
|
215 | figlet " Authing By:"
|
216 | echo
|
217 | echo "WECHATY_TOKEN=$WECHATY_TOKEN "
|
218 | echo
|
219 |
|
220 | pushd /wechaty
|
221 | npm run io-client
|
222 | popd
|
223 | }
|
224 |
|
225 | function wechaty::help() {
|
226 | figlet " Docker Usage: "
|
227 | cat <<HELP
|
228 |
|
229 |
|
230 |
|
231 | Usage: wechaty [ mybot.js | mybot.ts | COMMAND ]
|
232 |
|
233 | Run a JavaScript/TypeScript <Bot File>, or a <Wechaty Command>.
|
234 |
|
235 | <Bot File>:
|
236 | mybot.js: a JavaScript program for your bot.
|
237 | mybot.ts: a TypeScript program for your bot.
|
238 |
|
239 | <Commands>:
|
240 | demo Run Wechaty DEMO
|
241 | doctor Print Diagnose Report
|
242 | test Run Unit Test
|
243 |
|
244 | Learn more at:
|
245 | https://github.com/wechaty/wechaty/wiki/Docker
|
246 |
|
247 |
|
248 |
|
249 | HELP
|
250 | }
|
251 |
|
252 | function main() {
|
253 |
|
254 | echo -e 'nameserver 114.114.114.114\nnameserver 1.1.1.1\nnameserver 8.8.8.8' | sudo tee -a /etc/resolv.conf > /dev/null
|
255 |
|
256 | wechaty::banner
|
257 | figlet Connecting
|
258 | figlet ChatBots
|
259 |
|
260 | wechaty::printEnv
|
261 |
|
262 | VERSION=$(WECHATY_LOG=WARN wechaty-version 2>/dev/null || echo '0.0.0(unknown)')
|
263 |
|
264 | echo
|
265 | echo -n "Starting Docker Container for Wechaty v$VERSION with "
|
266 | echo -n "Node.js $(node --version) ..."
|
267 | echo
|
268 |
|
269 | local -i ret=0
|
270 |
|
271 | local defaultArg=help
|
272 | if [ -n "$WECHATY_TOKEN" ]; then
|
273 | defaultArg=io-client
|
274 | fi
|
275 |
|
276 | case "${1:-${defaultArg}}" in
|
277 |
|
278 |
|
279 |
|
280 | shell | sh | bash)
|
281 | /bin/bash -s || ret=$?
|
282 | ;;
|
283 |
|
284 |
|
285 |
|
286 |
|
287 | *.ts | *.js)
|
288 |
|
289 |
|
290 | wechaty::runBot "$@" || ret=$?
|
291 | ;;
|
292 |
|
293 |
|
294 |
|
295 |
|
296 | npm)
|
297 | shift
|
298 | npm "$@" || ret=$?
|
299 | ;;
|
300 |
|
301 | help|version)
|
302 | wechaty::help
|
303 | ;;
|
304 |
|
305 | io-client)
|
306 | wechaty::io-client
|
307 | ;;
|
308 |
|
309 | test)
|
310 |
|
311 | if [ -f "$HOME"/package.json ]; then
|
312 | pushd "$HOME"
|
313 | else
|
314 | pushd /wechaty
|
315 | fi
|
316 | WECHATY_LOG=silent npm run test
|
317 | popd
|
318 | ;;
|
319 |
|
320 |
|
321 |
|
322 |
|
323 | *)
|
324 | [ "$1" = "run" ] && shift
|
325 | npm run "$@" || ret=$?
|
326 | ;;
|
327 | esac
|
328 |
|
329 | wechaty::banner
|
330 | figlet " Exit $ret "
|
331 | return $ret
|
332 | }
|
333 |
|
334 | main "$@"
|
335 |
|