|
|
@@ -0,0 +1,356 @@
|
|
|
+@echo off
|
|
|
+chcp 65001 >nul
|
|
|
+setlocal enabledelayedexpansion
|
|
|
+
|
|
|
+REM WeChat Work Bot Message Sender - Windows Batch Version
|
|
|
+REM Version: 2.1 - Fixed file upload
|
|
|
+REM Support multiple message types
|
|
|
+
|
|
|
+set "CONFIG_FILE=%USERPROFILE%\.wecom_config.bat"
|
|
|
+set "TEMP_JSON=%TEMP%\wecom_temp.json"
|
|
|
+set "TEMP_RESPONSE=%TEMP%\wecom_response.json"
|
|
|
+
|
|
|
+REM Color definitions
|
|
|
+set "COLOR_INFO=echo [96m[INFO][0m"
|
|
|
+set "COLOR_SUCCESS=echo [92m[SUCCESS][0m"
|
|
|
+set "COLOR_ERROR=echo [91m[ERROR][0m"
|
|
|
+set "COLOR_WARNING=echo [93m[WARNING][0m"
|
|
|
+set "COLOR_TITLE=echo [95m"
|
|
|
+
|
|
|
+REM Check if curl is available
|
|
|
+where curl >nul 2>&1
|
|
|
+if errorlevel 1 (
|
|
|
+ %COLOR_ERROR% curl command not available! Please install curl or ensure it's in PATH
|
|
|
+ pause
|
|
|
+ exit /b 1
|
|
|
+)
|
|
|
+
|
|
|
+REM Load configuration
|
|
|
+call :load_config
|
|
|
+
|
|
|
+REM Parse command line arguments
|
|
|
+if "%1"=="" goto :show_menu
|
|
|
+if "%1"=="-t" goto :send_text_cmd
|
|
|
+if "%1"=="--text" goto :send_text_cmd
|
|
|
+if "%1"=="-f" goto :send_file_cmd
|
|
|
+if "%1"=="--file" goto :send_file_cmd
|
|
|
+if "%1"=="-m" goto :send_markdown_cmd
|
|
|
+if "%1"=="--markdown" goto :send_markdown_cmd
|
|
|
+if "%1"=="-c" goto :send_combo_cmd
|
|
|
+if "%1"=="--combo" goto :send_combo_cmd
|
|
|
+if "%1"=="--config" goto :setup_webhook
|
|
|
+if "%1"=="--test" goto :test_connection
|
|
|
+if "%1"=="-h" goto :show_help
|
|
|
+if "%1"=="--help" goto :show_help
|
|
|
+
|
|
|
+%COLOR_ERROR% Unknown parameter: %1
|
|
|
+goto :show_help
|
|
|
+
|
|
|
+:load_config
|
|
|
+if exist "%CONFIG_FILE%" (
|
|
|
+ call "%CONFIG_FILE%"
|
|
|
+ if defined WEBHOOK_KEY (
|
|
|
+ %COLOR_INFO% Loaded webhook key from config file
|
|
|
+ call :setup_urls
|
|
|
+ goto :eof
|
|
|
+ )
|
|
|
+)
|
|
|
+%COLOR_WARNING% Config file not found or webhook key is empty
|
|
|
+call :setup_webhook
|
|
|
+goto :eof
|
|
|
+
|
|
|
+:setup_webhook
|
|
|
+%COLOR_TITLE% === WeChat Work Bot Configuration ===[0m
|
|
|
+echo Please enter your WeChat Work bot webhook key:
|
|
|
+echo (Extract key part from https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=YOUR_KEY)
|
|
|
+echo.
|
|
|
+set /p "webhook_key=Webhook Key: "
|
|
|
+
|
|
|
+if "!webhook_key!"=="" (
|
|
|
+ %COLOR_ERROR% Webhook key cannot be empty!
|
|
|
+ pause
|
|
|
+ exit /b 1
|
|
|
+)
|
|
|
+
|
|
|
+REM Save configuration
|
|
|
+echo set "WEBHOOK_KEY=!webhook_key!" > "%CONFIG_FILE%"
|
|
|
+%COLOR_SUCCESS% Configuration saved to %CONFIG_FILE%
|
|
|
+
|
|
|
+set "WEBHOOK_KEY=!webhook_key!"
|
|
|
+call :setup_urls
|
|
|
+goto :eof
|
|
|
+
|
|
|
+:setup_urls
|
|
|
+set "BASE_URL=https://qyapi.weixin.qq.com/cgi-bin/webhook"
|
|
|
+set "SEND_URL=!BASE_URL!/send?key=!WEBHOOK_KEY!"
|
|
|
+set "UPLOAD_URL=!BASE_URL!/upload_media?key=!WEBHOOK_KEY!"
|
|
|
+goto :eof
|
|
|
+
|
|
|
+:send_text_cmd
|
|
|
+set "text_content=%~2"
|
|
|
+set "mention=%~3"
|
|
|
+if "!text_content!"=="" (
|
|
|
+ %COLOR_ERROR% Text content cannot be empty
|
|
|
+ exit /b 1
|
|
|
+)
|
|
|
+call :send_text "!text_content!" "!mention!"
|
|
|
+goto :eof
|
|
|
+
|
|
|
+:send_file_cmd
|
|
|
+set "file_path=%~2"
|
|
|
+if "!file_path!"=="" (
|
|
|
+ %COLOR_ERROR% File path cannot be empty
|
|
|
+ exit /b 1
|
|
|
+)
|
|
|
+call :send_file "!file_path!"
|
|
|
+goto :eof
|
|
|
+
|
|
|
+:send_markdown_cmd
|
|
|
+set "markdown_content=%~2"
|
|
|
+if "!markdown_content!"=="" (
|
|
|
+ %COLOR_ERROR% Markdown content cannot be empty
|
|
|
+ exit /b 1
|
|
|
+)
|
|
|
+call :send_markdown "!markdown_content!"
|
|
|
+goto :eof
|
|
|
+
|
|
|
+:send_combo_cmd
|
|
|
+set "text_content=%~2"
|
|
|
+set "file_path=%~3"
|
|
|
+if "!text_content!"=="" (
|
|
|
+ %COLOR_ERROR% Text content cannot be empty
|
|
|
+ exit /b 1
|
|
|
+)
|
|
|
+if "!file_path!"=="" (
|
|
|
+ %COLOR_ERROR% File path cannot be empty
|
|
|
+ exit /b 1
|
|
|
+)
|
|
|
+call :send_combo "!text_content!" "!file_path!"
|
|
|
+goto :eof
|
|
|
+
|
|
|
+:send_text
|
|
|
+set "content=%~1"
|
|
|
+set "mention=%~2"
|
|
|
+
|
|
|
+if "!mention!"==" " (
|
|
|
+ echo {"msgtype": "text","text": {"content": "!content!"}} > "!TEMP_JSON!"
|
|
|
+) else (
|
|
|
+ echo {"msgtype": "text","text": {"content": "!content!","mentioned_list": ["!mention!"]}} > "!TEMP_JSON!"
|
|
|
+)
|
|
|
+
|
|
|
+call :send_request "!TEMP_JSON!" "Text Message"
|
|
|
+goto :eof
|
|
|
+
|
|
|
+:send_file
|
|
|
+set "file_path=%~1"
|
|
|
+
|
|
|
+if not exist "!file_path!" (
|
|
|
+ %COLOR_ERROR% File does not exist: !file_path!
|
|
|
+ goto :eof
|
|
|
+)
|
|
|
+
|
|
|
+%COLOR_INFO% Uploading file: !file_path!
|
|
|
+
|
|
|
+REM Upload file and save response to temp file
|
|
|
+curl -s -X POST "!UPLOAD_URL!&type=file" -F "media=@!file_path!" -o "!TEMP_RESPONSE!"
|
|
|
+
|
|
|
+REM Check if response file exists and has content
|
|
|
+if not exist "!TEMP_RESPONSE!" (
|
|
|
+ %COLOR_ERROR% Upload request failed - no response received
|
|
|
+ goto :eof
|
|
|
+)
|
|
|
+
|
|
|
+REM Read response from file
|
|
|
+set "upload_response="
|
|
|
+for /f "usebackq delims=" %%i in ("!TEMP_RESPONSE!") do (
|
|
|
+ set "upload_response=!upload_response!%%i"
|
|
|
+)
|
|
|
+
|
|
|
+REM Debug: Show response
|
|
|
+%COLOR_INFO% Upload response: !upload_response!
|
|
|
+
|
|
|
+REM Check for success
|
|
|
+echo !upload_response! | findstr "\"errcode\":0" >nul
|
|
|
+if errorlevel 1 (
|
|
|
+ %COLOR_ERROR% File upload failed: !upload_response!
|
|
|
+ if exist "!TEMP_RESPONSE!" del "!TEMP_RESPONSE!" >nul 2>&1
|
|
|
+ goto :eof
|
|
|
+)
|
|
|
+
|
|
|
+REM Use PowerShell to extract media_id (more reliable JSON parsing)
|
|
|
+for /f "usebackq delims=" %%i in (`powershell -Command "try { $json = Get-Content '!TEMP_RESPONSE!' | ConvertFrom-Json; $json.media_id } catch { 'ERROR' }"`) do (
|
|
|
+ set "media_id=%%i"
|
|
|
+)
|
|
|
+
|
|
|
+if "!media_id!"=="ERROR" (
|
|
|
+ %COLOR_ERROR% Failed to parse media_id from response
|
|
|
+ if exist "!TEMP_RESPONSE!" del "!TEMP_RESPONSE!" >nul 2>&1
|
|
|
+ goto :eof
|
|
|
+)
|
|
|
+
|
|
|
+if "!media_id!"=="" (
|
|
|
+ %COLOR_ERROR% media_id is empty
|
|
|
+ if exist "!TEMP_RESPONSE!" del "!TEMP_RESPONSE!" >nul 2>&1
|
|
|
+ goto :eof
|
|
|
+)
|
|
|
+
|
|
|
+%COLOR_SUCCESS% File upload successful, media_id: !media_id!
|
|
|
+
|
|
|
+REM Send file message
|
|
|
+echo {"msgtype": "file","file": {"media_id": "!media_id!"}} > "!TEMP_JSON!"
|
|
|
+call :send_request "!TEMP_JSON!" "File Message"
|
|
|
+
|
|
|
+REM Clean up temp files
|
|
|
+if exist "!TEMP_RESPONSE!" del "!TEMP_RESPONSE!" >nul 2>&1
|
|
|
+goto :eof
|
|
|
+
|
|
|
+:send_markdown
|
|
|
+set "content=%~1"
|
|
|
+echo {"msgtype": "markdown","markdown": {"content": "!content!"}} > "!TEMP_JSON!"
|
|
|
+call :send_request "!TEMP_JSON!" "Markdown Message"
|
|
|
+goto :eof
|
|
|
+
|
|
|
+:send_combo
|
|
|
+set "text_content=%~1"
|
|
|
+set "file_path=%~2"
|
|
|
+
|
|
|
+%COLOR_INFO% Starting combo message send (text+file)
|
|
|
+call :send_text "!text_content!"
|
|
|
+timeout /t 2 /nobreak >nul
|
|
|
+call :send_file "!file_path!"
|
|
|
+%COLOR_SUCCESS% Combo message sent successfully!
|
|
|
+goto :eof
|
|
|
+
|
|
|
+:send_request
|
|
|
+set "json_file=%~1"
|
|
|
+set "msg_type=%~2"
|
|
|
+
|
|
|
+%COLOR_INFO% Sending !msg_type!...
|
|
|
+
|
|
|
+for /f "delims=" %%i in ('curl -s -X POST "!SEND_URL!" -H "Content-Type: application/json" -d @"!json_file!"') do set "response=%%i"
|
|
|
+
|
|
|
+echo !response! | findstr "\"errcode\":0" >nul
|
|
|
+if errorlevel 1 (
|
|
|
+ %COLOR_ERROR% !msg_type! send failed: !response!
|
|
|
+) else (
|
|
|
+ %COLOR_SUCCESS% !msg_type! sent successfully!
|
|
|
+)
|
|
|
+
|
|
|
+if exist "!json_file!" del "!json_file!" >nul 2>&1
|
|
|
+goto :eof
|
|
|
+
|
|
|
+:test_connection
|
|
|
+%COLOR_INFO% Testing bot connection...
|
|
|
+for /f "tokens=1-6 delims=/ " %%a in ("%date%") do set "current_date=%%c-%%a-%%b"
|
|
|
+for /f "tokens=1-2 delims=: " %%a in ("%time%") do set "current_time=%%a:%%b"
|
|
|
+set "test_msg=Robot connection test - !current_date! !current_time!"
|
|
|
+
|
|
|
+call :send_text "!test_msg!"
|
|
|
+goto :eof
|
|
|
+
|
|
|
+:show_menu
|
|
|
+cls
|
|
|
+%COLOR_TITLE% ==================================[0m
|
|
|
+%COLOR_TITLE% WeChat Work Bot Sender v2.1[0m
|
|
|
+%COLOR_TITLE% Windows Batch Version[0m
|
|
|
+%COLOR_TITLE% ==================================[0m
|
|
|
+echo 1. Send text message
|
|
|
+echo 2. Send file
|
|
|
+echo 3. Send text+file combo
|
|
|
+echo 4. Send Markdown message
|
|
|
+echo 5. Test connection
|
|
|
+echo 6. Reconfigure webhook key
|
|
|
+echo 7. Show help
|
|
|
+echo 8. Exit
|
|
|
+%COLOR_TITLE% ==================================[0m
|
|
|
+set /p "choice=Please select operation (1-8): "
|
|
|
+
|
|
|
+if "!choice!"=="1" goto :menu_text
|
|
|
+if "!choice!"=="2" goto :menu_file
|
|
|
+if "!choice!"=="3" goto :menu_combo
|
|
|
+if "!choice!"=="4" goto :menu_markdown
|
|
|
+if "!choice!"=="5" goto :menu_test
|
|
|
+if "!choice!"=="6" goto :menu_config
|
|
|
+if "!choice!"=="7" goto :show_help
|
|
|
+if "!choice!"=="8" goto :menu_exit
|
|
|
+
|
|
|
+%COLOR_ERROR% Invalid choice, please try again
|
|
|
+pause
|
|
|
+goto :show_menu
|
|
|
+
|
|
|
+:menu_text
|
|
|
+echo.
|
|
|
+set /p "text_input=Please enter text content: "
|
|
|
+set /p "mention_input=Mention someone (optional, press Enter to skip): "
|
|
|
+call :send_text "!text_input!" "!mention_input!"
|
|
|
+pause
|
|
|
+goto :show_menu
|
|
|
+
|
|
|
+:menu_file
|
|
|
+echo.
|
|
|
+set /p "file_input=Please enter file path: "
|
|
|
+call :send_file "!file_input!"
|
|
|
+pause
|
|
|
+goto :show_menu
|
|
|
+
|
|
|
+:menu_combo
|
|
|
+echo.
|
|
|
+set /p "text_input=Please enter text content: "
|
|
|
+set /p "file_input=Please enter file path: "
|
|
|
+call :send_combo "!text_input!" "!file_input!"
|
|
|
+pause
|
|
|
+goto :show_menu
|
|
|
+
|
|
|
+:menu_markdown
|
|
|
+echo.
|
|
|
+set /p "markdown_input=Please enter Markdown content: "
|
|
|
+call :send_markdown "!markdown_input!"
|
|
|
+pause
|
|
|
+goto :show_menu
|
|
|
+
|
|
|
+:menu_test
|
|
|
+echo.
|
|
|
+call :test_connection
|
|
|
+pause
|
|
|
+goto :show_menu
|
|
|
+
|
|
|
+:menu_config
|
|
|
+echo.
|
|
|
+call :setup_webhook
|
|
|
+call :setup_urls
|
|
|
+pause
|
|
|
+goto :show_menu
|
|
|
+
|
|
|
+:menu_exit
|
|
|
+%COLOR_INFO% Goodbye!
|
|
|
+exit /b 0
|
|
|
+
|
|
|
+:show_help
|
|
|
+%COLOR_TITLE% WeChat Work Bot Message Sender - Windows Batch Version v2.1[0m
|
|
|
+echo.
|
|
|
+echo Usage: %~nx0 [options]
|
|
|
+echo.
|
|
|
+echo Options:
|
|
|
+echo -t, --text TEXT Send text message
|
|
|
+echo -f, --file FILE Send file
|
|
|
+echo -c, --combo TEXT FILE Send text+file combo
|
|
|
+echo -m, --markdown TEXT Send Markdown message
|
|
|
+echo --config Reconfigure webhook key
|
|
|
+echo --test Test connection
|
|
|
+echo -h, --help Show this help information
|
|
|
+echo.
|
|
|
+echo Examples:
|
|
|
+echo %~nx0 -t "Hello World" # Send text message
|
|
|
+echo %~nx0 -f "report.pdf" # Send file
|
|
|
+echo %~nx0 -c "Please check attachment" "doc.pdf" # Send text+file
|
|
|
+echo %~nx0 -m "# Title\n**Bold text**" # Send Markdown
|
|
|
+echo %~nx0 # Interactive menu
|
|
|
+echo.
|
|
|
+echo Config file location: %CONFIG_FILE%
|
|
|
+echo.
|
|
|
+echo Note:
|
|
|
+echo - Uses PowerShell for JSON parsing (more reliable)
|
|
|
+echo - System needs curl command installed
|
|
|
+echo - Supports files with Chinese names
|
|
|
+pause
|
|
|
+goto :eof
|