logo
1
1
Login
Forkfromcnb/plugins/tencentcom/dingtalk-bot-msg, behind:main1 commits

dingtalk-bot-msg

Send DingTalk robot messages, supporting all DingTalk robot message types.

Usage in Cloud-Native Builds

main: push: - stages: - name: dingtalk-bot-msg imports: https://xxx/envs.yaml image: tencentcom/dingtalk-bot-msg:latest settings: content: "your message" c_type: "text" # Supported: text, markdown, link, actionCard, multiActionCard, feedCard secret: $SECRET webhook: $WEBHOOK at: "199xxxxxx" # Multiple numbers separated by semicolons (;) isAtAll: false debug: false # New: Enable debug mode

Example envs.yaml file:

WEBHOOK: xxx SECRET: xxx

Message Types Usage Guide

1. Text Message (text)

c_type: "text" content: "Text message content"

2. Markdown Message (markdown)

c_type: "markdown" content: "#### Markdown Title\n- Item 1\n- Item 2"

3. Link Message (link)

c_type: "link" content: "Title|Message content|Image URL|Jump URL"

4. ActionCard with Single Button (actionCard)

c_type: "actionCard" content: "Title|Message content|Button orientation(0-vertical,1-horizontal)|Button title|Jump URL"

5. ActionCard with Multiple Buttons (multiActionCard)

c_type: "multiActionCard" content: "Title|Message content|Button orientation(0-vertical,1-horizontal)|Button1 title,Button1 URL|Button2 title,Button2 URL"

6. FeedCard Message (feedCard)

c_type: "feedCard" content: "Title1,Jump URL1,Image URL1|Title2,Jump URL2,Image URL2"

Parameter Description

  • content: Message content, format varies according to message type

  • c_type: Message type. Supported: text, markdown, link, actionCard, multiActionCard, feedCard

  • webhook: DingTalk robot WebHook [Need to create in DingTalk PC client]. Reference Documentation

  • secret: Secret key for security settings and signing. Optional (recommended). Reference Documentation

  • at: People to be mentioned. Enter the mobile numbers of the people to be mentioned, separated by ";" for multiple.

  • isAtAll: Whether to mention everyone. Boolean value.

  • debug: Whether to enable debug mode. Boolean value, default is false. When enabled, it will display complete request and response information, including sensitive information.

Debug Mode Description

The newly added debug mode can help users better troubleshoot issues during development and testing:

  1. Features:

    • Displays the complete webhook URL (without hiding the token part)
    • Shows the complete secret key (without replacing with asterisks)
    • Shows complete phone numbers (without hiding the middle part)
    • Displays detailed signature generation process (timestamp and signature value)
    • Shows complete request content and response content
  2. Usage:

    • Add debug: true parameter in configuration
    • Or use --debug true parameter in command line
  3. Use Cases:

    • Troubleshooting issues during development and testing
    • Verifying if the signature algorithm is correct
    • Checking complete request and response content
  4. Security Tips:

    • Debug mode displays sensitive information, do not enable in production environment
    • Debug logs may contain sensitive information like secrets and tokens, ensure log security

Complete Implementation Description

  1. Support for All DingTalk Robot Message Types:
    • Text message (text)
    • Markdown message (markdown)
    • Link message (link)
    • ActionCard with single button (actionCard)
    • ActionCard with multiple buttons (multiActionCard)
    • FeedCard message (feedCard)
  2. Parameter Processing Optimization:
    • Using pipe character (|) to separate multiple parameters for complex message types
    • For multi-item messages (like FeedCard), supporting pipe character to separate multiple items
  3. Error Handling:
    • Added format validation for various message types
    • Providing clearer error prompts
  4. Compatibility:
    • Maintaining compatibility with original implementation
    • New features do not affect the use of original text and markdown messages
  5. Debug Functionality:
    • Added debug mode for easier development, testing, and troubleshooting
    • Provides sensitive information protection mechanism, automatically hiding sensitive information in non-debug mode

This extended implementation covers all message types supported by DingTalk robots and provides clear documentation and test cases for users to easily use various message types.

About the Test Script

Optimization Notes

  1. Parameter Precise Matching:

    • Only text and markdown type messages contain PLUGIN_AT and PLUGIN_ISATALL parameters
    • Other message types do not contain these parameters as they do not support @ functionality

    Therefore, the entry file entrypoint.sh has been optimized

  2. Enhanced Readability:

    • Added separators and test type descriptions
    • Used more realistic test message content
  3. Parameter Completeness:

    • Each message type includes all parameters it supports
    • For optional parameters (like SECRET), retained in some tests and omitted in others to test different scenarios
  4. Unified Format:

    • All message types' content format matches the parsing logic in the code exactly
    • Using consistent parameter naming style
  5. Error Prevention:

    • Avoiding passing AT parameters in message types that don't support @ functionality
    • Ensuring each message's parameter combination is valid
  6. Debug Support:

    • Added support for debug parameter in the test script
    • Some test cases enable debug mode to verify the debug functionality works properly

This test script can now comprehensively verify the correctness of various message types while conforming to the specification requirements of the DingTalk robot API.

Plugin Entry File Changes

The current entrypoint.sh script has been optimized to conditionally pass parameters based on message type and support debug mode:

#!/bin/sh # Basic parameters ARGS="--content \"$PLUGIN_CONTENT\" \ --c_type \"$PLUGIN_C_TYPE\" \ --webhook \"$PLUGIN_WEBHOOK\"" # Add secret parameter (if exists) if [ -n "$PLUGIN_SECRET" ]; then ARGS="$ARGS --secret \"$PLUGIN_SECRET\"" fi # Only text and markdown messages support @ functionality case "$PLUGIN_C_TYPE" in "text"|"markdown") # Add at parameter (if exists) if [ -n "$PLUGIN_AT" ]; then ARGS="$ARGS --at \"$PLUGIN_AT\"" fi # Add isAtAll parameter (default is false) ARGS="$ARGS --isAtAll ${PLUGIN_ISATALL:-false}" ;; esac # Finally add debug parameter ARGS="$ARGS --debug \"${PLUGIN_DEBUG:-false}\"" # Execute Go program eval "go run /plugins/main.go $ARGS"

Entry File Optimization

  1. Conditional Parameter Passing:
    • Only text and markdown message types will pass --at and --isAtAll parameters
    • Other message types will not pass these parameters
  2. Parameter Building Method:
    • Using variables to gradually build parameter list
    • Using eval to finally execute the command, correctly handling parameters with spaces
  3. Optional Parameter Handling:
    • secret parameter is only passed when provided
    • at parameter is only passed when provided (only for supported message types)
  4. Default Value Handling:
    • isAtAll maintains default value as false (only for supported message types)
    • debug parameter defaults to false
  5. Security:
    • All parameters are wrapped in double quotes to prevent issues with spaces or special characters
    • Ensuring parameters are properly escaped before using eval

Notes

  1. link, actionCard, multiActionCard, and feedCard type messages do not support @ functionality
  2. Special characters in message content need to be properly escaped
  3. Each message type has different size limits, please refer to DingTalk official documentation
  4. Debug mode displays sensitive information, do not enable in production environment
  5. Using signing method (secret parameter) can improve security, recommended to enable

For more usage, refer to: DingTalk Open Platform Help Documentation

About

发送钉钉机器人消息插件

Language
Go64.2%
Markdown24.3%
License5.2%
Shell4.7%
Others1.6%