DingtalkChatbot Documentation

repository·master·Indexed 23 days ago

https://github.com/zhuifengshen/dingtalkchatbot

A Python wrapper for the DingTalk custom robot API. It enables developers to send various message types to DingTalk groups, including Text, Markdown, Link, Image, FeedCard, and ActionCard, for automation, alerts, and notifications.

Tokens
886
Snippets
1
Records
9
Agent score
30%

What's inside DingtalkChatbot

  1. Install DingtalkChatbot via pip

    master

    Install the DingtalkChatbot package using pip. This library provides a Python wrapper for various DingTalk custom robot message types (Text, Link, Image, Markdown, ActionCard, etc.).

    To install:

    pip install DingtalkChatbot

    To upgrade to the latest version:

    pip install -U DingtalkChatbot
    pip install DingtalkChatbot
  2. Troubleshooting and Best Practices

    master

    Image Protocol

    If images do not appear in the DingTalk Web version, ensure you are using https protocol URLs. http URLs may only work in the desktop client.

    By default, links open in the system browser (pc_slide=False). To open links in the DingTalk PC client's sidebar, set pc_slide=True during DingtalkChatbot initialization.

    Mentioning Users (@)

    By default, the at_mobiles list appends the mobile numbers to the end of the message text to trigger the @ effect. If you want to control exactly where the @ appears in your text, set is_auto_at=False and manually include the @mobile_number in your message string.

    Limitations

    • No Response Mechanism: Standard custom robots do not support responding to users (e.g., when a user @ the robot). For interactive/replying capabilities, you must use DingTalk's Outgoing Robot (Enterprise-created chatbot).
  3. Send Text messages

    master

    Use send_text to send plain text messages. You can mention everyone in the group or mention specific users by their mobile numbers.

    • msg: The text content.
    • is_at_all: Set to True to @all.
    • at_mobiles: A list of mobile numbers (strings or integers) to @ specific users.
    • is_auto_at: If False, the mobile numbers in at_mobiles will not be automatically appended to the end of the message text.
  4. Initialize the DingtalkChatbot client

    master

    To use the library, you must first initialize the DingtalkChatbot class with your DingTalk Webhook URL. You can also optionally provide a secret if you have enabled the 'Signing' (加签) option in your DingTalk robot settings. Additionally, you can set pc_slide=True to make links open in the DingTalk PC client sidebar instead of a browser.

    Initialization patterns:

    • Standard: DingtalkChatbot(webhook)
    • With Signing (Secret): DingtalkChatbot(webhook, secret=secret)
    • With Sidebar Links: DingtalkChatbot(webhook, pc_slide=True)
  5. Send ActionCard messages

    master

    ActionCards allow you to send messages with interactive buttons. You can use ActionCard to create different types of interaction layouts.

    • Global ActionCard: A single button that triggers an action.
    • Select ActionCard (Double Option): Two buttons (e.g., 'Support' and 'Oppose').
    • Select ActionCard (List Option): Multiple buttons (e.g., 'Support', 'Neutral', 'Oppose').

    Use CardItem to define the buttons (btns).

  6. Send Markdown messages

    master

    Use send_markdown to send rich text formatted with Markdown. You can also use @all or @mobiles with Markdown messages.

    • title: The title of the message.
    • text: The Markdown formatted string.
    • is_at_all: Set to True to @all.
    • at_mobiles: List of mobile numbers to @ specific users.