Installation:
composer require aping/pdding-robot
or add to composer.json:
"require": {
"aping/pdding-robot": "^1.0"
}
First Usage: Initialize the client with your DingTalk robot token and secret:
use Aping\PddingRobot\Fast;
$robot = Fast::new('your_robot_token', 'your_signature_secret');
Send First Message:
$response = $robot->sendText('Hello, this is my first DingTalk message!');
Fast class: Core class for sending messages.Sending Notifications:
$robot->sendText("Deployment failed on staging! Check logs: [link](https://example.com/logs)");
Structured Alerts (Markdown):
$robot->sendMarkdown(
"System Alert",
"#### **Database Connection Issue**\n\n- **Status**: Critical\n- **Time**: " . now() . "\n- **Action**: Restart service or check config\n\n> "
);
Interactive Cards (ActionCard):
$robot->sendSingleActionCard(
"New Feature Release",
"The v2.0 release is live! Key improvements:\n- Faster API responses\n- New dashboard UI\n\n",
"View Changelog",
"https://example.com/changelog"
);
Multi-Option Cards:
$robot->sendMultiActionCard(
"Deploy Options",
"Select environment for deployment:",
[
['title' => 'Staging', 'actionURL' => 'https://staging.example.com/deploy'],
['title' => 'Production', 'actionURL' => 'https://prod.example.com/deploy'],
]
);
Feed Cards (Multiple Items):
$robot->sendFeedCard([
[
'title' => 'Server #1',
'messageUrl' => 'https://monitor.example.com/server/1',
'picUrl' => 'https://example.com/server1.png',
],
[
'title' => 'Server #2',
'messageUrl' => 'https://monitor.example.com/server/2',
'picUrl' => 'https://example.com/server2.png',
],
]);
Environment Configuration:
Store tokens/secrets in .env:
DINGTALK_ROBOT_TOKEN=your_token_here
DINGTALK_SECRET=your_secret_here
Load via Laravel's config() helper:
$robot = Fast::new(config('services.dingtalk.token'), config('services.dingtalk.secret'));
Error Handling:
try {
$response = $robot->sendText("Critical Alert");
if (!$response->isOk()) {
Log::error("DingTalk Error: " . $response->getError());
}
} catch (\Exception $e) {
Log::error("DingTalk SDK Error: " . $e->getMessage());
}
Rate Limiting:
$messages = collect([...]);
$messages->chunk(5)->each(function ($chunk) use ($robot) {
foreach ($chunk as $message) {
$robot->sendText($message);
}
sleep(1); // Avoid hitting rate limits
});
Dynamic Content: Use Laravel Blade or templating for dynamic messages:
$robot->sendMarkdown(
"Build Status",
view('notifications.dingtalk.build_status', ['build' => $build])->render()
);
Webhook Integration: Trigger messages from Laravel events or jobs:
// In an Event Listener
public function handle(DeploymentFailed $event) {
$robot = app(Fast::class);
$robot->sendText("Deployment failed: " . $event->error);
}
Secret Validation:
Fast::new(token, '')).Message Length Limits:
messageUrl.Image URLs:
@lADOpwk3K80C0M0FoA for internal files).https:// for public images or upload to DingTalk’s internal file system.Response Handling:
isOk() returns true for HTTP 200, but DingTalk may still return a non-success payload (e.g., errcode: 1).$response->getRaw() for DingTalk’s errcode field:$data = json_decode($response->getRaw(), true);
if ($data['errcode'] !== 0) {
// Handle DingTalk-specific errors
}
Deprecation:
Enable Verbose Logging:
$robot = Fast::new(token, secret, [
'debug' => true, // Enable if supported (check source)
]);
$response = $robot->sendText("Test");
Log::debug("DingTalk Response:", $response->getRaw());
Test with Postman:
POST https://oapi.dingtalk.com/robot/send?access_token=YOUR_TOKEN
Headers:
Content-Type: application/json
Body:
{
"msgtype": "text",
"text": { "content": "Hello" }
}
Common Errors:
errcode: 8: Invalid access_token (check token permissions in DingTalk admin).errcode: 14: Invalid signature (ensure secret matches DingTalk settings).errcode: 10001: Rate limit exceeded (add delays between requests).Custom Message Types:
Fast class to add unsupported message types (e.g., sendFile):class ExtendedFast extends Fast {
public function sendFile($fileUrl) {
$payload = [
'msgtype' => 'file',
'file' => ['file_url' => $fileUrl],
];
return $this->send($payload);
}
}
Middleware for Requests:
class DingTalkMiddleware {
public function send(Fast $robot, $payload) {
$payload['agent_id'] = 1; // Add custom fields
return $robot->send($payload);
}
}
Queue Jobs:
class SendDingTalkJob implements ShouldQueue {
public function handle() {
$robot = new Fast(config('dingtalk.token'), config('dingtalk.secret'));
$robot->sendText("Queued Message");
}
}
**F
How can I help you explore Laravel packages today?