|
| 1 | +#!/usr/bin/env bash |
| 2 | +# |
| 3 | +# PHP-Wechaty - Connect ChatBots |
| 4 | +# |
| 5 | +# https://github.com/wechaty/php-wechaty |
| 6 | +# |
| 7 | +set -e |
| 8 | + |
| 9 | +export HOME=/bot |
| 10 | +export PATH=$PATH:/php-wechaty/bin:/php-wechaty/vendor |
| 11 | + |
| 12 | +function php-wechaty::banner() { |
| 13 | + echo |
| 14 | + figlet " PHP-Wechaty " |
| 15 | + echo ____________________________________________________ |
| 16 | + echo " https://github.com/zhangchunsheng" |
| 17 | +} |
| 18 | + |
| 19 | +function php-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 | + Troubleshooting: |
| 30 | + 1. Did you bind the current directory into container? |
| 31 | + check your `docker run ...` command, if there's no `volumn` arg, |
| 32 | + then you need to add it so that we can bind the volume of /bot: |
| 33 | + `--volume="$(pwd)":/bot` |
| 34 | + this will let the container visit your current directory. |
| 35 | + 2. Are you sure your .php files aren't .php.txt? |
| 36 | + this could be a problem on new Windows installs (file |
| 37 | + extensions hidden by default). |
| 38 | + if you still have issue, please have a look at |
| 39 | + https://github.com/wechaty/php-wechaty |
| 40 | + and do a search in issues, that might be help. |
| 41 | +TROUBLESHOOTING |
| 42 | +} |
| 43 | + |
| 44 | +function php-wechaty::printEnv () { |
| 45 | + num=$(env | grep -c 'PHP-WECHATY') |
| 46 | + echo "PHP-WECHATY Environment Variables: $num" |
| 47 | + env | grep 'PHP-WECHATY' |
| 48 | +} |
| 49 | + |
| 50 | +function php-wechaty::errorCtrlC () { |
| 51 | + # http://www.tldp.org/LDP/abs/html/exitcodes.html |
| 52 | + # 130 Script terminated by Control-C Ctl-C Control-C is fatal error signal 2, (130 = 128 + 2, see above) |
| 53 | + echo ' Script terminated by Control-C ' |
| 54 | + figlet ' Ctrl + C ' |
| 55 | +} |
| 56 | + |
| 57 | +function php-wechaty::pressEnterToContinue() { |
| 58 | + local -i timeoutSecond=${1:-30} |
| 59 | + local message=${2:-'Press ENTER to continue ... '} |
| 60 | + |
| 61 | + read -r -t "$timeoutSecond" -p "$message" || true |
| 62 | + echo |
| 63 | +} |
| 64 | + |
| 65 | +function php-wechaty::diagnose() { |
| 66 | + local -i ret=$1 && shift |
| 67 | + local file=$1 && shift |
| 68 | + |
| 69 | + echo "ERROR: Bot exited with code $ret" |
| 70 | + |
| 71 | + figlet ' BUG REPORT ' |
| 72 | + php-wechaty::pressEnterToContinue 30 |
| 73 | + |
| 74 | + echo |
| 75 | + echo "### 1. source code of $file" |
| 76 | + echo |
| 77 | + cat "$HOME/$file" || echo "ERROR: file not found" |
| 78 | + echo |
| 79 | + |
| 80 | + echo |
| 81 | + echo "### 2. directory structor of $HOME" |
| 82 | + echo |
| 83 | + ls -l "$HOME" |
| 84 | + |
| 85 | + echo |
| 86 | + echo '### 3. composer.json' |
| 87 | + echo |
| 88 | + cat "$HOME"/composer.json || echo "No composer.json" |
| 89 | + |
| 90 | + echo |
| 91 | + echo "### 4. directory structor inside $HOME/vendor" |
| 92 | + echo |
| 93 | + ls "$HOME"/vendor || echo "No vendor" |
| 94 | + |
| 95 | + echo |
| 96 | + echo '### 5. php-wechaty doctor' |
| 97 | + echo |
| 98 | + php-wechaty-doctor |
| 99 | + |
| 100 | + figlet " Submit a ISSUE " |
| 101 | + echo _____________________________________________________________ |
| 102 | + echo '####### please paste all the above diagnose messages #######' |
| 103 | + echo |
| 104 | + echo 'Wechaty Issue https://github.com/wechaty/php-wechaty/issues' |
| 105 | + echo |
| 106 | + |
| 107 | + php-wechaty::pressEnterToContinue |
| 108 | +} |
| 109 | + |
| 110 | +function php-wechaty::runBot() { |
| 111 | + local botFile=$1 |
| 112 | + |
| 113 | + if [ ! -f "$HOME/$botFile" ]; then |
| 114 | + php-wechaty::errorBotNotFound "$botFile" |
| 115 | + return 1 |
| 116 | + fi |
| 117 | + |
| 118 | + echo "Working directory: $HOME" |
| 119 | + cd "$HOME" |
| 120 | + |
| 121 | + [ -f composer.json ] && { |
| 122 | + # echo "Install dependencies modules ..." |
| 123 | + |
| 124 | + # |
| 125 | + # |
| 126 | + echo "Please make sure you had installed all the composer modules which is depended on your bot script." |
| 127 | + } |
| 128 | + |
| 129 | + |
| 130 | + local -i ret=0 |
| 131 | + case "$botFile" in |
| 132 | + *.php) |
| 133 | + echo "Executing node $*" |
| 134 | + php \ |
| 135 | + "$@" \ |
| 136 | + & |
| 137 | + ;; |
| 138 | + *) |
| 139 | + echo "ERROR: php-wechaty::runBot() no php file" |
| 140 | + exit 1 & |
| 141 | + esac |
| 142 | + |
| 143 | + wait "$!" || ret=$? # fix `can only `return' from a function or sourced script` error |
| 144 | + |
| 145 | + case "$ret" in |
| 146 | + 0) |
| 147 | + ;; |
| 148 | + 130) |
| 149 | + php-wechaty::errorCtrlC |
| 150 | + ;; |
| 151 | + *) |
| 152 | + php-wechaty::diagnose "$ret" "$@" |
| 153 | + ;; |
| 154 | + esac |
| 155 | + |
| 156 | + return "$ret" |
| 157 | +} |
| 158 | + |
| 159 | +function php-wechaty::help() { |
| 160 | + figlet " Docker Usage: " |
| 161 | + cat <<HELP |
| 162 | + Usage: php-wechaty [ mybot.php | COMMAND ] |
| 163 | + Run a PHP <Bot File>, or a <PHP-Wechaty Command>. |
| 164 | + <Bot File>: |
| 165 | + mybot.php: a php program for your bot. |
| 166 | + <Commands>: |
| 167 | + demo Run PHP-Wechaty DEMO |
| 168 | + doctor Print Diagnose Report |
| 169 | + test Run Unit Test |
| 170 | + Learn more at: |
| 171 | + https://github.com/wechaty/wechaty/wiki/Docker |
| 172 | +HELP |
| 173 | +} |
| 174 | + |
| 175 | +function main() { |
| 176 | + # issue #84 |
| 177 | + 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 |
| 178 | + |
| 179 | + wechaty::banner |
| 180 | + figlet Connecting |
| 181 | + figlet ChatBots |
| 182 | + |
| 183 | + wechaty::printEnv |
| 184 | + |
| 185 | + VERSION=$(PHP-WECHATY_LOG=WARN php-wechaty-version 2>/dev/null || echo '0.0.0(unknown)') |
| 186 | + |
| 187 | + echo |
| 188 | + echo -n "Starting Docker Container for PHP-Wechaty v$VERSION with " |
| 189 | + echo -n "php $(php --version) ..." |
| 190 | + echo |
| 191 | + |
| 192 | + local -i ret=0 |
| 193 | + |
| 194 | + local defaultArg=help |
| 195 | + |
| 196 | + case "${1:-${defaultArg}}" in |
| 197 | + # |
| 198 | + # 1. Get a shell |
| 199 | + # |
| 200 | + shell | sh | bash) |
| 201 | + /bin/bash -s || ret=$? |
| 202 | + ;; |
| 203 | + |
| 204 | + # |
| 205 | + # 2. Run a bot |
| 206 | + # |
| 207 | + *.php) |
| 208 | + wechaty::runBot "$@" || ret=$? |
| 209 | + ;; |
| 210 | + |
| 211 | + # |
| 212 | + # 3. If there's additional `npm` arg... |
| 213 | + # |
| 214 | + php) |
| 215 | + shift |
| 216 | + php "$@" || ret=$? |
| 217 | + ;; |
| 218 | + |
| 219 | + help|version) |
| 220 | + php-wechaty::help |
| 221 | + ;; |
| 222 | + |
| 223 | + # |
| 224 | + # 4. Default to execute php ... |
| 225 | + # |
| 226 | + *) |
| 227 | + php "$@" || ret=$? |
| 228 | + ;; |
| 229 | + esac |
| 230 | + |
| 231 | + php-wechaty::banner |
| 232 | + figlet " Exit $ret " |
| 233 | + return $ret |
| 234 | +} |
| 235 | + |
| 236 | +main "$@" |
0 commit comments