I. Introduction of Dingding custom robot

Nail robot is an advanced extension function of Nail group, but it is very simple to use, just need to register a nail account, you can aggregate third-party service information into the nail group, to achieve automatic synchronization of information.

Common usage scenarios:

  • 1. Integrate Github, Gitlab and other source management services to realize source update synchronization;
  • 2. Integrated Trello, JIRA and other project coordination services to realize project information synchronization;
  • 3, the robot supports Webhook custom access, so more possibilities can be realized, such as operation and maintenance alarm, automatic test result report, work and life schedule (punching in at work, eating after work, fitness, reading, birthday, anniversary…). The warning;

Currently, custom robots support three message formats: text, link and Markdown, and five message types. For details, please refer to the official document of custom robots

Two, installation and use

Such a good function, just add a robot to the pin group, get the Webhoo address. Here’s a sneak peek at the command line:

curl 'https://oapi.dingtalk.com/robot/send?access_token=xxxxxxxx' \
   -H 'Content-Type: application/json' \
   -d '{" msgtype ", "text", "text" : {" content ":" I am who I am, it is not the same as fireworks "}}' 
Copy the code

Due to various message calls, the official only provides the Encapsulation of Java language, but USUALLY uses Python. In order to facilitate the use of automation projects at ordinary times, I spent some time on the weekend to encapsulate various message types with Python language. The code has been open source on GitHub, and PyPI has also been uploaded.

DingtalkChatbot 2, installation command is as follows:

pip install DingtalkChatbot
Copy the code

3. Supported functions are as follows:

  • Support Text messages;
  • Support Link messages;
  • Support Markdown messages;
  • Support ActionCard message;
  • Support automatic notification when message sending fails;
  • Python2 and Python3 are supported.

Three, each message type usage example

from dingtalkchatbot.chatbot import DingtalkChatbot
# WebHook address
webhook = 'https://oapi.dingtalk.com/robot/send?access_token= here to fill out their nailing group of custom token'
# Initialize robot Ding
xiaoding = DtalkChatbot(webhook)
# Text message @all
xiaoding.send_text(msg='I am Xiaoding, Xiaoding is me! ', is_at_all=True)
Copy the code

# Text Specifies the @user of the message
at_mobiles = ['Fill in the phone number of the user you want to be reminded of, string or number']
xiaoding.send_text(msg='I am Xiaoding, Xiaoding is me! ', at_mobiles=at_mobiles)
Copy the code

# Link message
xiaoding.send_link(title='To my surprise, a certain lu unexpectedly... ', text='Here's the story... ', message_url='http://www.kwongwah.com.my/?p=454748", pic_url="https://pbs.twimg.com/media/CEwj7EDWgAE5eIF.jpg')
Copy the code

# Markdown message @everyone
xiaoding.send_markdown(title='Oxygen text', text='#### Guangzhou weather \n'
                           '> 9 degrees, nw wind force 1, air quality 89, relative temperature 73%\n\n'
                           '>! [view] (http://www.sinaimg.cn/dy/slidenews/5_img/2013_28/453_28488_469248.jpg) \ n '
                           '> # # # # # # 10 p.m release [weather] (http://www.thinkpage.cn/) \ n',
                           is_at_all=True)
Copy the code

    # Markdown message @specified user
    xiaoding.send_markdown(title='Oxygen text', text='#### Guangzhou weather @ user phone number \n'
                           '> 9 degrees, nw wind force 1, air quality 89, relative temperature 73%\n\n'
                           '>! [view] (http://www.sinaimg.cn/dy/slidenews/5_img/2013_28/453_28488_469248.jpg) \ n '
                           '> # # # # # # 10 p.m release [weather] (http://www.thinkpage.cn/) \ n',
                           at_mobiles=at_mobiles)
Copy the code

# FeedCard Message type
feedlink1 = FeedLink(title="Oxygen Girl", message_url="https://www.dingtalk.com/", pic_url="https://unzippedtv.com/wp-content/uploads/sites/28/2016/02/asian.jpg")
feedlink2 = FeedLink(title="Oxygen Eyed Beauty.", message_url="https://www.dingtalk.com/", pic_url="https://unzippedtv.com/wp-content/uploads/sites/28/2016/02/asian.jpg")
feedlink3 = FeedLink(title="Beauty of oxygen", message_url="https://www.dingtalk.com/", pic_url="https://unzippedtv.com/wp-content/uploads/sites/28/2016/02/asian.jpg")
links = [feedlink1.get_data(), feedlink2.get_data(), feedlink3.get_data()]
xiaoding.send_feed_card(links)
Copy the code

ActionCard Indicates the type of the whole jump message
btns1 = [{"title": "View details"."actionURL": "https://www.dingtalk.com/"}]
actioncard1 = ActionCard(title='To my surprise... ',
                             text='! \ [option] (http://www.songshan.es/wp-content/uploads/2016/01/Yin-Yang.png) n# # # story is like this... ',
                             btns=btns1,
                             btn_orientation=1,
                             hide_avatar=1)
xiaoding.send_action_card(actioncard1)
Copy the code

# ActionCard separate jump message type (two options)
btns2 = [{"title": "Support"."actionURL": "https://www.dingtalk.com/"}, {"title": "Against"."actionURL": "http://www.back china.com/news/2018/01/11/537468.html"}]
actioncard2 = ActionCard(title='To my surprise... ',
                             text='! \ [option] (http://www.songshan.es/wp-content/uploads/2016/01/Yin-Yang.png) n# # # story is like this... ',
                             btns=btns2,
                             btn_orientation=1,
                             hide_avatar=1)
xiaoding.send_action_card(actioncard2)
Copy the code

# ActionCard separate jump message type (list option)
btns3 = [{"title": "Support"."actionURL": "https://www.dingtalk.com/"}, {"title": "Neutral"."actionURL": "https://www.dingtalk.com/"}, {"title": "Against"."actionURL": "https://www.dingtalk.com/"}]
    actioncard3 = ActionCard(title='To my surprise... ',
                             text='! \ [option] (http://www.songshan.es/wp-content/uploads/2016/01/Yin-Yang.png) n# # # story is like this... ',
                             btns=btns3,
                             btn_orientation=1,
                             hide_avatar=1)
xiaoding.send_action_card(actioncard3)
Copy the code

Dude, use more scenarios, now let’s imagine… > star <