My composer package “nail custom robot”

Time:2020-9-24

This is after watching the “lx2 PHP expansion pack practical tutorial – from introduction to release” course by Overtrue, and I started to toss it out. However, unit testing and automated testing have not been written yet. This part has just been learned and is still struggling.

Source code address: overnaive / dingtalkrobot

Requirement

  • PHP >= 7.2.5
  • Composer

Installation

composer require overnaive/dingtalkrobot ^1.0

Documents

Nailing official documents

Usage

<?php
use DingTalkRobot\DingTalkRobot;

//Instantiation
$robot = new DingTalkRobot([
    'access_token' => 'access_token_string',
    'secret' => 'secret_string',
]);

//Elegant call
$result = $robot->message->text
    ->Settextcontent ('I am me, it's not the same Fireworks @ 156xxx8827 ')
    ->addAtMobile('156xxxx8827')
    ->send();

//Native call
$result = $robot->message
    ->send([
        'msgtype' => 'text',
        'text' => [
            'content' = > I am me, it's different fireworks @156xxx8827',
        ],
        'atMobiles' => [
            '156xxxx8827'
        ],
        'isAtAll' => false,
    ]);

Example

<?php
use DingTalkRobot\DingTalkRobot;

//Instantiation
$robot = new DingTalkRobot([
    'access_token' => 'access_token_string',
    'secret' => 'secret_string',
]);

//Text type
$result = $robot->message->text
    ->Settextcontent ('I am me, it's a different fireworks @156xxx8827')
    ->addAtMobile('156xxxx8827')
    ->addAtMobile('189xxxx8325')
    ->send();

var_dump($result);

//Link type
$result = $robot->message->link
    ->Settitle ('the train of the times goes forward ')
    ->Settext ('This new version to be released soon, founder XX calls it mangrove forest. Before that, whenever faced with a major upgrade, product managers would use a code name for the occasion. This time, why mangrove
    ->setMessageUrl('https://www.dingtalk.com/s?__biz=MzA4NjMwMTA2Ng==&mid=2650316842&idx=1&sn=60da3ea2b29f1dcc43a7c8e4a7c97a16&scene=2&srcid=09189AnRJEdIiWVaKltFzNTw&from=timeline&isappinstalled=0&key=&ascene=2&uin=&devicetype=android-23&version=26031933&nettype=WIFI')
    ->send();

var_dump($result);

//Markdown type
$result = $robot->message->markdown
    ->Settitle ('hangzhou weather ')
    ->Settext ("ාාාාාාාාාාාාාාාාාාාාාාාාාාා( https://imgs.developpaper.com/imgs/TB1NwmBEL9TBuNjy1zbXXXpepXa-2400-1218.png )\n> Released at 10:20 [weather]( https://www.dingtalk.com ) \n")
    ->addAtMobile('150XXXXXXXX')
    ->send();

var_dump($result);

//Overall jump actioncard type
$result = $robot->message->actionCard
    ->"Jobs wanted to build an apple Cafe 20 years ago, and it was the predecessor of the apple store."
    ->setText("![screenshot](https://imgs.developpaper.com/imgs/TB1ut3xxbsrBKNjSZFpXXcXhFXa-846-786.png)
 ###Apple Cafe jobs wanted to build 20 years ago
 The design of Apple store is moving from being full of technology to life, and its trend of life can be traced back to Apple's plan to build a coffee shop 20 years ago. "
    ->Addbtn ('read the full text ',' https://www.dingtalk.com/ )
    ->verticalBtn()
    ->send();

var_dump($result);

//Independent jump actioncard type
$result = $robot->message->actionCard
    ->"Jobs wanted to build an apple Cafe 20 years ago, and it was the predecessor of the apple store."
    ->setText("![screenshot](https://imgs.developpaper.com/imgs/TB1ut3xxbsrBKNjSZFpXXcXhFXa-846-786.png)
 ###Apple Cafe jobs wanted to build 20 years ago
 The design of Apple store is moving from being full of technology to life, and its trend of life can be traced back to Apple's plan to build a coffee shop 20 years ago. "
    ->verticalBtn()
    ->Addbtn ('good content ',' https://www.dingtalk.com/ )
    ->Addbtn ('not interested ',' https://www.dingtalk.com/ )
    ->send();

var_dump($result);

//Feedcard type
$result = $robot->message->feedCard
    ->addLink(
        The train of the times is moving forward,
        'https://www.dingtalk.com/s?__biz=MzA4NjMwMTA2Ng==&mid=2650316842&idx=1&sn=60da3ea2b29f1dcc43a7c8e4a7c97a16&scene=2&srcid=09189AnRJEdIiWVaKltFzNTw&from=timeline&isappinstalled=0&key=&ascene=2&uin=&devicetype=android-23&version=26031933&nettype=WIFI',
        'https://imgs.developpaper.com/imgs/TB1ayl9mpYqK1RjSZLeXXbXppXa-170-62.png'
    )
    ->addLink(
        The train of the times is moving forward 2 ',
        'https://www.dingtalk.com/s?__biz=MzA4NjMwMTA2Ng==&mid=2650316842&idx=1&sn=60da3ea2b29f1dcc43a7c8e4a7c97a16&scene=2&srcid=09189AnRJEdIiWVaKltFzNTw&from=timeline&isappinstalled=0&key=&ascene=2&uin=&devicetype=android-23&version=26031933&nettype=WIFI',
        'https://imgs.developpaper.com/imgs/TB1ayl9mpYqK1RjSZLeXXbXppXa-170-62.png'
    )
    ->send();

var_dump($result);

Advanced Usage

Custom guzzle client

Replace guzzle client

<?php
use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Handler\CurlHandler;
use DingTalkRobot\DingTalkRobot;

//Instantiation
$robot = new DingTalkRobot([
    'access_token' => 'access_token_string',
    'secret' => 'secret_string',
]);

//Custom handler, the following configuration code from the official document
$handler = new CurlHandler();
$stack = HandlerStack::create($handler);
$customClient = new Client(['handler' => $stack]);

//Replace the guzzle client with a reserved method
$message = $robot->message->setHttpClient($customClient);
$result = $message->text
    ->Settextcontent ('I am me, it's a different fireworks @156xxx8827')
    ->addAtMobile('156xxxx8827')
    ->send();

Configure the guzzle client

<?php
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Handler\CurlHandler;
use DingTalkRobot\DingTalkRobot;

//Custom handler, the following configuration code from the official document
$handler = new CurlHandler();
$stack = HandlerStack::create($handler);

//Instantiation, via the fuzzy_ Options from configuration client
$robot = new DingTalkRobot([
    'access_token' => 'access_token_string',
    'secret' => 'secret_string',
    'guzzle_options' => [
        'handler' = > $stack, // custom handler
        'timeout '= > 10.0, // set request timeout
    ],
]);

$result = $robot->message->text
    ->Settextcontent ('I am me, it's not the same Fireworks @ 156xxx8827 ')
    ->addAtMobile('156xxxx8827')
    ->send();

In theory, coprocessing is supported by replacing the guzzle client or configuring the handler above, which has not been tested.

For more guzzle configurations, please refer to: guzzle documentation

License

MIT

Recommended Today

SQL server cannot generate fruncm thread

SQL Server was unable to generate the fruncm thread. The database error log is as follows: Copy codeThe code is as follows: 2013-09-26 21:21:50.31 Server      Microsoft SQL Server 2005 – 9.00.1399.06 (Intel X86)Oct 14 2005 00:33:37 Copyright (c) 1988-2005 Microsoft CorporationEnterprise Edition on Windows NT 5.2 (Build 3790: Service Pack 2) 2013-09-26 21:21:50.31 Server      (c) […]