wecom_reboot_no_model.bat 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. @echo off
  2. chcp 65001 >nul
  3. setlocal enabledelayedexpansion
  4. REM WeChat Work Bot Message Sender - Windows Batch Version
  5. REM Version: 2.1 - Fixed file upload
  6. REM Support multiple message types
  7. set "CONFIG_FILE=%USERPROFILE%\.wecom_config.bat"
  8. set "TEMP_JSON=%TEMP%\wecom_temp.json"
  9. set "TEMP_RESPONSE=%TEMP%\wecom_response.json"
  10. REM Color definitions
  11. set "COLOR_INFO=echo [96m[INFO][0m"
  12. set "COLOR_SUCCESS=echo [92m[SUCCESS][0m"
  13. set "COLOR_ERROR=echo [91m[ERROR][0m"
  14. set "COLOR_WARNING=echo [93m[WARNING][0m"
  15. set "COLOR_TITLE=echo [95m"
  16. REM Check if curl is available
  17. where curl >nul 2>&1
  18. if errorlevel 1 (
  19. %COLOR_ERROR% curl command not available! Please install curl or ensure it's in PATH
  20. pause
  21. exit /b 1
  22. )
  23. REM Load configuration
  24. call :load_config
  25. REM Parse command line arguments
  26. if "%1"=="" goto :show_menu
  27. if "%1"=="-t" goto :send_text_cmd
  28. if "%1"=="--text" goto :send_text_cmd
  29. if "%1"=="-f" goto :send_file_cmd
  30. if "%1"=="--file" goto :send_file_cmd
  31. if "%1"=="-m" goto :send_markdown_cmd
  32. if "%1"=="--markdown" goto :send_markdown_cmd
  33. if "%1"=="-c" goto :send_combo_cmd
  34. if "%1"=="--combo" goto :send_combo_cmd
  35. if "%1"=="--config" goto :setup_webhook
  36. if "%1"=="--test" goto :test_connection
  37. if "%1"=="-h" goto :show_help
  38. if "%1"=="--help" goto :show_help
  39. %COLOR_ERROR% Unknown parameter: %1
  40. goto :show_help
  41. :load_config
  42. if exist "%CONFIG_FILE%" (
  43. call "%CONFIG_FILE%"
  44. if defined WEBHOOK_KEY (
  45. %COLOR_INFO% Loaded webhook key from config file
  46. call :setup_urls
  47. goto :eof
  48. )
  49. )
  50. %COLOR_WARNING% Config file not found or webhook key is empty
  51. call :setup_webhook
  52. goto :eof
  53. :setup_webhook
  54. %COLOR_TITLE% === WeChat Work Bot Configuration ===[0m
  55. echo Please enter your WeChat Work bot webhook key:
  56. echo (Extract key part from https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=YOUR_KEY)
  57. echo.
  58. set /p "webhook_key=Webhook Key: "
  59. if "!webhook_key!"=="" (
  60. %COLOR_ERROR% Webhook key cannot be empty!
  61. pause
  62. exit /b 1
  63. )
  64. REM Save configuration
  65. echo set "WEBHOOK_KEY=!webhook_key!" > "%CONFIG_FILE%"
  66. %COLOR_SUCCESS% Configuration saved to %CONFIG_FILE%
  67. set "WEBHOOK_KEY=!webhook_key!"
  68. call :setup_urls
  69. goto :eof
  70. :setup_urls
  71. set "BASE_URL=https://qyapi.weixin.qq.com/cgi-bin/webhook"
  72. set "SEND_URL=!BASE_URL!/send?key=!WEBHOOK_KEY!"
  73. set "UPLOAD_URL=!BASE_URL!/upload_media?key=!WEBHOOK_KEY!"
  74. goto :eof
  75. :send_text_cmd
  76. set "text_content=%~2"
  77. set "mention=%~3"
  78. if "!text_content!"=="" (
  79. %COLOR_ERROR% Text content cannot be empty
  80. exit /b 1
  81. )
  82. call :send_text "!text_content!" "!mention!"
  83. goto :eof
  84. :send_file_cmd
  85. set "file_path=%~2"
  86. if "!file_path!"=="" (
  87. %COLOR_ERROR% File path cannot be empty
  88. exit /b 1
  89. )
  90. call :send_file "!file_path!"
  91. goto :eof
  92. :send_markdown_cmd
  93. set "markdown_content=%~2"
  94. if "!markdown_content!"=="" (
  95. %COLOR_ERROR% Markdown content cannot be empty
  96. exit /b 1
  97. )
  98. call :send_markdown "!markdown_content!"
  99. goto :eof
  100. :send_combo_cmd
  101. set "text_content=%~2"
  102. set "file_path=%~3"
  103. if "!text_content!"=="" (
  104. %COLOR_ERROR% Text content cannot be empty
  105. exit /b 1
  106. )
  107. if "!file_path!"=="" (
  108. %COLOR_ERROR% File path cannot be empty
  109. exit /b 1
  110. )
  111. call :send_combo "!text_content!" "!file_path!"
  112. goto :eof
  113. :send_text
  114. set "content=%~1"
  115. set "mention=%~2"
  116. if "!mention!"==" " (
  117. echo {"msgtype": "text","text": {"content": "!content!"}} > "!TEMP_JSON!"
  118. ) else (
  119. echo {"msgtype": "text","text": {"content": "!content!","mentioned_list": ["!mention!"]}} > "!TEMP_JSON!"
  120. )
  121. call :send_request "!TEMP_JSON!" "Text Message"
  122. goto :eof
  123. :send_file
  124. set "file_path=%~1"
  125. if not exist "!file_path!" (
  126. %COLOR_ERROR% File does not exist: !file_path!
  127. goto :eof
  128. )
  129. %COLOR_INFO% Uploading file: !file_path!
  130. REM Upload file and save response to temp file
  131. curl -s -X POST "!UPLOAD_URL!&type=file" -F "media=@!file_path!" -o "!TEMP_RESPONSE!"
  132. REM Check if response file exists and has content
  133. if not exist "!TEMP_RESPONSE!" (
  134. %COLOR_ERROR% Upload request failed - no response received
  135. goto :eof
  136. )
  137. REM Read response from file
  138. set "upload_response="
  139. for /f "usebackq delims=" %%i in ("!TEMP_RESPONSE!") do (
  140. set "upload_response=!upload_response!%%i"
  141. )
  142. REM Debug: Show response
  143. %COLOR_INFO% Upload response: !upload_response!
  144. REM Check for success
  145. echo !upload_response! | findstr "\"errcode\":0" >nul
  146. if errorlevel 1 (
  147. %COLOR_ERROR% File upload failed: !upload_response!
  148. if exist "!TEMP_RESPONSE!" del "!TEMP_RESPONSE!" >nul 2>&1
  149. goto :eof
  150. )
  151. REM Use PowerShell to extract media_id (more reliable JSON parsing)
  152. for /f "usebackq delims=" %%i in (`powershell -Command "try { $json = Get-Content '!TEMP_RESPONSE!' | ConvertFrom-Json; $json.media_id } catch { 'ERROR' }"`) do (
  153. set "media_id=%%i"
  154. )
  155. if "!media_id!"=="ERROR" (
  156. %COLOR_ERROR% Failed to parse media_id from response
  157. if exist "!TEMP_RESPONSE!" del "!TEMP_RESPONSE!" >nul 2>&1
  158. goto :eof
  159. )
  160. if "!media_id!"=="" (
  161. %COLOR_ERROR% media_id is empty
  162. if exist "!TEMP_RESPONSE!" del "!TEMP_RESPONSE!" >nul 2>&1
  163. goto :eof
  164. )
  165. %COLOR_SUCCESS% File upload successful, media_id: !media_id!
  166. REM Send file message
  167. echo {"msgtype": "file","file": {"media_id": "!media_id!"}} > "!TEMP_JSON!"
  168. call :send_request "!TEMP_JSON!" "File Message"
  169. REM Clean up temp files
  170. if exist "!TEMP_RESPONSE!" del "!TEMP_RESPONSE!" >nul 2>&1
  171. goto :eof
  172. :send_markdown
  173. set "content=%~1"
  174. echo {"msgtype": "markdown","markdown": {"content": "!content!"}} > "!TEMP_JSON!"
  175. call :send_request "!TEMP_JSON!" "Markdown Message"
  176. goto :eof
  177. :send_combo
  178. set "text_content=%~1"
  179. set "file_path=%~2"
  180. %COLOR_INFO% Starting combo message send (text+file)
  181. call :send_text "!text_content!"
  182. timeout /t 2 /nobreak >nul
  183. call :send_file "!file_path!"
  184. %COLOR_SUCCESS% Combo message sent successfully!
  185. goto :eof
  186. :send_request
  187. set "json_file=%~1"
  188. set "msg_type=%~2"
  189. %COLOR_INFO% Sending !msg_type!...
  190. for /f "delims=" %%i in ('curl -s -X POST "!SEND_URL!" -H "Content-Type: application/json" -d @"!json_file!"') do set "response=%%i"
  191. echo !response! | findstr "\"errcode\":0" >nul
  192. if errorlevel 1 (
  193. %COLOR_ERROR% !msg_type! send failed: !response!
  194. ) else (
  195. %COLOR_SUCCESS% !msg_type! sent successfully!
  196. )
  197. if exist "!json_file!" del "!json_file!" >nul 2>&1
  198. goto :eof
  199. :test_connection
  200. %COLOR_INFO% Testing bot connection...
  201. for /f "tokens=1-6 delims=/ " %%a in ("%date%") do set "current_date=%%c-%%a-%%b"
  202. for /f "tokens=1-2 delims=: " %%a in ("%time%") do set "current_time=%%a:%%b"
  203. set "test_msg=Robot connection test - !current_date! !current_time!"
  204. call :send_text "!test_msg!"
  205. goto :eof
  206. :show_menu
  207. cls
  208. %COLOR_TITLE% ==================================[0m
  209. %COLOR_TITLE% WeChat Work Bot Sender v2.1[0m
  210. %COLOR_TITLE% Windows Batch Version[0m
  211. %COLOR_TITLE% ==================================[0m
  212. echo 1. Send text message
  213. echo 2. Send file
  214. echo 3. Send text+file combo
  215. echo 4. Send Markdown message
  216. echo 5. Test connection
  217. echo 6. Reconfigure webhook key
  218. echo 7. Show help
  219. echo 8. Exit
  220. %COLOR_TITLE% ==================================[0m
  221. set /p "choice=Please select operation (1-8): "
  222. if "!choice!"=="1" goto :menu_text
  223. if "!choice!"=="2" goto :menu_file
  224. if "!choice!"=="3" goto :menu_combo
  225. if "!choice!"=="4" goto :menu_markdown
  226. if "!choice!"=="5" goto :menu_test
  227. if "!choice!"=="6" goto :menu_config
  228. if "!choice!"=="7" goto :show_help
  229. if "!choice!"=="8" goto :menu_exit
  230. %COLOR_ERROR% Invalid choice, please try again
  231. pause
  232. goto :show_menu
  233. :menu_text
  234. echo.
  235. set /p "text_input=Please enter text content: "
  236. set /p "mention_input=Mention someone (optional, press Enter to skip): "
  237. call :send_text "!text_input!" "!mention_input!"
  238. pause
  239. goto :show_menu
  240. :menu_file
  241. echo.
  242. set /p "file_input=Please enter file path: "
  243. call :send_file "!file_input!"
  244. pause
  245. goto :show_menu
  246. :menu_combo
  247. echo.
  248. set /p "text_input=Please enter text content: "
  249. set /p "file_input=Please enter file path: "
  250. call :send_combo "!text_input!" "!file_input!"
  251. pause
  252. goto :show_menu
  253. :menu_markdown
  254. echo.
  255. set /p "markdown_input=Please enter Markdown content: "
  256. call :send_markdown "!markdown_input!"
  257. pause
  258. goto :show_menu
  259. :menu_test
  260. echo.
  261. call :test_connection
  262. pause
  263. goto :show_menu
  264. :menu_config
  265. echo.
  266. call :setup_webhook
  267. call :setup_urls
  268. pause
  269. goto :show_menu
  270. :menu_exit
  271. %COLOR_INFO% Goodbye!
  272. exit /b 0
  273. :show_help
  274. %COLOR_TITLE% WeChat Work Bot Message Sender - Windows Batch Version v2.1[0m
  275. echo.
  276. echo Usage: %~nx0 [options]
  277. echo.
  278. echo Options:
  279. echo -t, --text TEXT Send text message
  280. echo -f, --file FILE Send file
  281. echo -c, --combo TEXT FILE Send text+file combo
  282. echo -m, --markdown TEXT Send Markdown message
  283. echo --config Reconfigure webhook key
  284. echo --test Test connection
  285. echo -h, --help Show this help information
  286. echo.
  287. echo Examples:
  288. echo %~nx0 -t "Hello World" # Send text message
  289. echo %~nx0 -f "report.pdf" # Send file
  290. echo %~nx0 -c "Please check attachment" "doc.pdf" # Send text+file
  291. echo %~nx0 -m "# Title\n**Bold text**" # Send Markdown
  292. echo %~nx0 # Interactive menu
  293. echo.
  294. echo Config file location: %CONFIG_FILE%
  295. echo.
  296. echo Note:
  297. echo - Uses PowerShell for JSON parsing (more reliable)
  298. echo - System needs curl command installed
  299. echo - Supports files with Chinese names
  300. pause
  301. goto :eof