Skip to content

Commit cc76977

Browse files
committed
refactor(zoho): Simplify Authenticator and enhance Message class
- Replaced AggregateAuthenticator with WebHookAuthenticator for cleaner implementation. - Added AsNullUri trait to Message class for improved URI handling. - Removed unused token generation method and related code from Authenticator. - Updated README with relevant Zoho CLIQ webhook links for better documentation. Signed-off-by: guanguans <[email protected]>
1 parent 541fca7 commit cc76977

File tree

6 files changed

+152
-130
lines changed

6 files changed

+152
-130
lines changed

src/ZohoCliq/Authenticator.php

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -13,36 +13,6 @@
1313

1414
namespace Guanguans\Notify\ZohoCliq;
1515

16-
use Guanguans\Notify\Foundation\Authenticators\AggregateAuthenticator;
17-
use Guanguans\Notify\Foundation\Authenticators\BearerAuthenticator;
18-
use Guanguans\Notify\Foundation\Authenticators\UriTemplateAuthenticator;
16+
use Guanguans\Notify\Foundation\Authenticators\WebHookAuthenticator;
1917

20-
class Authenticator extends AggregateAuthenticator
21-
{
22-
public function __construct(
23-
string $companyId,
24-
string $channelUniqueName,
25-
#[\SensitiveParameter]
26-
?string $authToken = null
27-
) {
28-
$authenticators = [
29-
new UriTemplateAuthenticator([
30-
'companyId' => $companyId,
31-
'channelUniqueName' => $channelUniqueName,
32-
]),
33-
];
34-
35-
if (null !== $authToken) {
36-
$authenticators[] = new BearerAuthenticator($authToken);
37-
}
38-
39-
parent::__construct(...$authenticators);
40-
}
41-
42-
public static function generateToken(string $clientId, string $clientSecret): string
43-
{
44-
$token = new Token($clientId, $clientSecret);
45-
46-
return $token->generateToken();
47-
}
48-
}
18+
class Authenticator extends WebHookAuthenticator {}

src/ZohoCliq/Client.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,27 @@
1414
namespace Guanguans\Notify\ZohoCliq;
1515

1616
/**
17+
* @see https://www.zoho.com/cliq/help/platform/webhook-tokens.html
18+
* @see https://cliq.zoho.com/integrations/webhook-tokens
1719
* @see https://www.zoho.com/cliq/help/restapi/v2/#Post_Message_Channel
18-
* @see https://www.zoho.com/cliq/help/platform/webhooks.html
20+
* @see https://www.zoho.com/cliq/help/restapi/v2/#Message_Object
21+
* @see https://www.zoho.com/cliq/help/search-results.html?query=webhook
22+
* @see https://www.zoho.com/cliq/help/restapi/v2/#authentication
23+
* @see https://github.com/Weble/ZohoClient
24+
* @see https://github.com/MarJose123/laravel-zoho-cliq-alert
25+
*
26+
* ```
27+
* curl --location 'https://cliq.zoho.com/api/v2/channelsbyname/announcements/message?zapikey=1001.4805235707f212af4b11be76483da614.d95141b05ae0550eabe503061a598' \
28+
* --header 'Content-Type: application/json' \
29+
* --data '{
30+
* "text": "Welcome to Agile Bot! I'\''m here to give you a brief on what Agile is all about."
31+
* }'
32+
* ```
1933
*/
2034
class Client extends \Guanguans\Notify\Foundation\Client
2135
{
2236
public function __construct(Authenticator $authenticator)
2337
{
2438
parent::__construct($authenticator);
25-
$this->baseUri('https://cliq.zoho.com/');
2639
}
2740
}

src/ZohoCliq/Messages/Message.php

Lines changed: 74 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313

1414
namespace Guanguans\Notify\ZohoCliq\Messages;
1515

16+
use Guanguans\Notify\Foundation\Concerns\AsNullUri;
17+
use Symfony\Component\OptionsResolver\OptionsResolver;
18+
1619
/**
1720
* @method self bot(array $bot)
1821
* @method self buttons(array $buttons)
@@ -23,24 +26,90 @@
2326
*/
2427
class Message extends \Guanguans\Notify\Foundation\Message
2528
{
29+
use AsNullUri;
30+
protected array $required = [
31+
// 'text',
32+
];
2633
protected array $defined = [
2734
'text',
2835
'bot',
2936
'card',
37+
'styles',
3038
'slides',
3139
'buttons',
32-
'styles',
3340
];
3441
protected array $allowedTypes = [
3542
'bot' => 'array',
3643
'card' => 'array',
37-
'slides' => 'array',
38-
'buttons' => 'array',
3944
'styles' => 'array',
45+
'slides' => 'array[]',
46+
'buttons' => 'array[]',
4047
];
48+
protected array $options = [
49+
'slides' => [],
50+
'buttons' => [],
51+
];
52+
53+
public function addSlide(array $slide): self
54+
{
55+
$this->options['slides'][] = $slide;
56+
57+
return $this;
58+
}
59+
60+
public function addButton(array $button): self
61+
{
62+
$this->options['buttons'][] = $button;
63+
64+
return $this;
65+
}
4166

42-
public function toHttpUri(): string
67+
protected function configureOptionsResolver(OptionsResolver $optionsResolver): void
4368
{
44-
return 'company/{companyId}/api/v2/channelsbyname/{channelUniqueName}/message';
69+
$optionsResolver
70+
->setDefault('bot', static function (OptionsResolver $optionsResolver): void {
71+
$optionsResolver
72+
->setDefined([
73+
'name',
74+
'image',
75+
]);
76+
})
77+
->setDefault('card', static function (OptionsResolver $optionsResolver): void {
78+
$optionsResolver
79+
->setDefined([
80+
'title',
81+
'theme',
82+
'thumbnail',
83+
'icon',
84+
'preview',
85+
]);
86+
})
87+
->setDefault('styles', static function (OptionsResolver $optionsResolver): void {
88+
$optionsResolver
89+
->setDefined([
90+
'highlight',
91+
])
92+
->setAllowedTypes('highlight', 'bool');
93+
})
94+
->setDefault('slides', static function (OptionsResolver $optionsResolver): void {
95+
$optionsResolver
96+
->setPrototype(true)
97+
->setDefined([
98+
'type',
99+
'title',
100+
'data',
101+
]);
102+
})
103+
->setDefault('buttons', static function (OptionsResolver $optionsResolver): void {
104+
$optionsResolver
105+
->setPrototype(true)
106+
->setDefined([
107+
'label',
108+
'hint',
109+
'key',
110+
'type',
111+
'action',
112+
]);
113+
});
45114
}
46115
}

src/ZohoCliq/README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
## Related links
66

7+
* [https://www.zoho.com/cliq/help/platform/webhook-tokens.html](https://www.zoho.com/cliq/help/platform/webhook-tokens.html)
8+
* [https://cliq.zoho.com/integrations/webhook-tokens](https://cliq.zoho.com/integrations/webhook-tokens)
79
* [https://www.zoho.com/cliq/help/restapi/v2/#Post_Message_Channel](https://www.zoho.com/cliq/help/restapi/v2/#Post_Message_Channel)
8-
* [https://www.zoho.com/cliq/help/platform/webhooks.html](https://www.zoho.com/cliq/help/platform/webhooks.html)
9-
* [https://www.zoho.com/cliq/help/restapi/v2/#Message_Cards](https://www.zoho.com/cliq/help/restapi/v2/#Message_Cards)
10+
* [https://www.zoho.com/cliq/help/restapi/v2/#Message_Object](https://www.zoho.com/cliq/help/restapi/v2/#Message_Object)

src/ZohoCliq/Token.php

Lines changed: 0 additions & 47 deletions
This file was deleted.

tests/ZohoCliq/ClientTest.php

Lines changed: 58 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -24,67 +24,83 @@
2424
use Guanguans\Notify\ZohoCliq\Messages\Message;
2525

2626
it('can send message', function (): void {
27-
// $token = Authenticator::generateToken('client-id', 'client-secret');
28-
$authenticator = new Authenticator('company-id', 'your-channel', 'use $token');
27+
$authenticator = new Authenticator(
28+
'https://cliq.zoho.com/api/v2/channelsbyname/announcements/message?zapikey=1001.4805235707f212af4b11be76483da614.d95141b05ae0550eabe503061a598'
29+
);
2930
$client = new Client($authenticator);
3031
$message = Message::make([
31-
'text' => 'This is a test message from ZohoCliq integration.',
32-
]);
33-
34-
expect($client)
35-
->mock([
36-
response(faker()->text()),
37-
])
38-
->assertCanSendMessage($message);
39-
})->group(__DIR__, __FILE__);
40-
41-
it('can send message with bot customization', function (): void {
42-
// $token = Authenticator::generateToken('client-id', 'client-secret');
43-
$authenticator = new Authenticator('company-id', 'your-channel', 'use $token');
44-
$client = new Client($authenticator);
45-
$message = Message::make([
46-
'text' => 'Hello from custom bot!',
32+
'text' => 'This is text.',
4733
'bot' => [
48-
'name' => 'Custom Bot',
49-
'image' => 'https://example.com/bot-icon.png',
34+
'name' => 'This is bot name.',
35+
'image' => 'https://www.zoho.com/cliq/help/restapi/images/bot-custom.png',
5036
],
51-
]);
52-
53-
expect($client)
54-
->mock([
55-
response(faker()->text()),
56-
])
57-
->assertCanSendMessage($message);
58-
})->group(__DIR__, __FILE__);
59-
60-
it('can send message with card', function (): void {
61-
// $token = Authenticator::generateToken('client-id', 'client-secret');
62-
$authenticator = new Authenticator('company-id', 'your-channel', 'use $token');
63-
$client = new Client($authenticator);
64-
$message = Message::make([
65-
'text' => 'Check out this notification!',
6637
'card' => [
67-
'title' => 'Important Update',
38+
'title' => 'This is card title.',
6839
'theme' => 'modern-inline',
69-
'thumbnail' => 'https://example.com/notification-icon.png',
40+
'thumbnail' => 'https://www.zoho.com/cliq/help/restapi/images/announce_icon.png',
41+
],
42+
'slides' => [
43+
[
44+
'type' => 'table',
45+
'title' => 'This is slide table title.',
46+
'data' => [
47+
'headers' => [
48+
'Name',
49+
'Team',
50+
'Reporting To',
51+
],
52+
'rows' => [
53+
[
54+
'Name' => 'Paula Rojas',
55+
'Team' => 'Zylker-Sales',
56+
'Reporting To' => 'Li Jung',
57+
],
58+
[
59+
'Name' => 'Quinn Rivers',
60+
'Team' => 'Zylker-Marketing',
61+
'Reporting To' => 'Patricia James',
62+
],
63+
],
64+
],
65+
],
7066
],
7167
'buttons' => [
7268
[
73-
'label' => 'View Details',
69+
'label' => 'View button',
7470
'type' => '+',
7571
'action' => [
76-
'type' => 'open.url',
72+
'type' => 'invoke.function',
7773
'data' => [
78-
'web' => 'https://example.com/details',
74+
'name' => 'internlist',
7975
],
8076
],
8177
],
8278
],
83-
]);
79+
])
80+
->addSlide([
81+
'type' => 'list',
82+
'title' => 'This is slide list title.',
83+
'data' => [
84+
'Time - Tracking for Tasks',
85+
'Prioritize requirements effectively',
86+
'Identify and work on a fix for bugs instantly',
87+
],
88+
])
89+
->addButton([
90+
'label' => 'Cancel button',
91+
'type' => '-',
92+
'action' => [
93+
'type' => 'invoke.function',
94+
'data' => [
95+
'name' => 'internlist',
96+
],
97+
],
98+
]);
8499

85100
expect($client)
86101
->mock([
87-
response(faker()->text()),
102+
response(status: 204),
103+
response('{"code":"oauthtoken_invalid","message":"Invalid OAuth token passed."}', 401),
88104
])
89105
->assertCanSendMessage($message);
90106
})->group(__DIR__, __FILE__);

0 commit comments

Comments
 (0)