Skip to content

[Notifier] Add Twitter notifier #48084

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@
use Symfony\Component\Notifier\Bridge\Telnyx\TelnyxTransportFactory;
use Symfony\Component\Notifier\Bridge\TurboSms\TurboSmsTransport;
use Symfony\Component\Notifier\Bridge\Twilio\TwilioTransportFactory;
use Symfony\Component\Notifier\Bridge\Twitter\TwitterTransportFactory;
use Symfony\Component\Notifier\Bridge\Vonage\VonageTransportFactory;
use Symfony\Component\Notifier\Bridge\Yunpian\YunpianTransportFactory;
use Symfony\Component\Notifier\Bridge\Zendesk\ZendeskTransportFactory;
Expand Down Expand Up @@ -2593,6 +2594,7 @@ private function registerNotifierConfiguration(array $config, ContainerBuilder $
TelnyxTransportFactory::class => 'notifier.transport_factory.telnyx',
TurboSmsTransport::class => 'notifier.transport_factory.turbo-sms',
TwilioTransportFactory::class => 'notifier.transport_factory.twilio',
TwitterTransportFactory::class => 'notifier.transport_factory.twitter',
VonageTransportFactory::class => 'notifier.transport_factory.vonage',
YunpianTransportFactory::class => 'notifier.transport_factory.yunpian',
ZendeskTransportFactory::class => 'notifier.transport_factory.zendesk',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
use Symfony\Component\Notifier\Bridge\Telnyx\TelnyxTransportFactory;
use Symfony\Component\Notifier\Bridge\TurboSms\TurboSmsTransportFactory;
use Symfony\Component\Notifier\Bridge\Twilio\TwilioTransportFactory;
use Symfony\Component\Notifier\Bridge\Twitter\TwitterTransportFactory;
use Symfony\Component\Notifier\Bridge\Vonage\VonageTransportFactory;
use Symfony\Component\Notifier\Bridge\Yunpian\YunpianTransportFactory;
use Symfony\Component\Notifier\Bridge\Zendesk\ZendeskTransportFactory;
Expand Down Expand Up @@ -105,6 +106,10 @@
->parent('notifier.transport_factory.abstract')
->tag('texter.transport_factory')

->set('notifier.transport_factory.twitter', TwitterTransportFactory::class)
->parent('notifier.transport_factory.abstract')
->tag('chatter.transport_factory')

->set('notifier.transport_factory.all-my-sms', AllMySmsTransportFactory::class)
->parent('notifier.transport_factory.abstract')
->tag('texter.transport_factory')
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/Tests export-ignore
/phpunit.xml.dist export-ignore
/.gitattributes export-ignore
/.gitignore export-ignore
3 changes: 3 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/Twitter/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
vendor/
composer.lock
phpunit.xml
7 changes: 7 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/Twitter/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
CHANGELOG
=========

6.3
---

* Add the bridge
19 changes: 19 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/Twitter/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2022 Fabien Potencier

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
25 changes: 25 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/Twitter/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Twitter Notifier
===============

Provides [Twitter](https://twitter.com) integration for Symfony Notifier.

DSN example
-----------

```
TWITTER_DSN=twitter://API_KEY:API_SECRET:ACCESS_TOKEN:ACCESS_SECRET@default
```

where:
- `API_KEY` is your Twitter API Key
- `API_SECRET` is your Twitter API Key Secret
- `ACCESS_TOKEN` is your read+write Twitter Access Token
- `ACCESS_SECRET` is your read+write Twitter Access Token Secret

Resources
---------

* [Contributing](https://symfony.com/doc/current/contributing/index.html)
* [Report issues](https://github.com/symfony/symfony/issues) and
[send Pull Requests](https://github.com/symfony/symfony/pulls)
in the [main Symfony repository](https://github.com/symfony/symfony)
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Notifier\Bridge\Twitter\Tests;

use Symfony\Component\Notifier\Bridge\Twitter\TwitterTransportFactory;
use Symfony\Component\Notifier\Test\TransportFactoryTestCase;

class TwitterTransportFactoryTest extends TransportFactoryTestCase
{
public function createFactory(): TwitterTransportFactory
{
return new TwitterTransportFactory();
}

public function createProvider(): iterable
{
yield ['twitter://host.test', 'twitter://A:B:C:D@host.test'];
}

public function supportsProvider(): iterable
{
yield [true, 'twitter://default'];
yield [false, 'somethingElse://default'];
}

public function unsupportedSchemeProvider(): iterable
{
yield ['somethingElse://default'];
}

public function incompleteDsnProvider(): iterable
{
yield ['twitter://A:B@default', 'Invalid "twitter://A:B@default" notifier DSN: Access Token is missing'];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Notifier\Bridge\Twitter\Tests;

use Symfony\Component\HttpClient\MockHttpClient;
use Symfony\Component\HttpClient\Response\MockResponse;
use Symfony\Component\Mime\Part\File;
use Symfony\Component\Notifier\Bridge\Twitter\TwitterOptions;
use Symfony\Component\Notifier\Bridge\Twitter\TwitterTransport;
use Symfony\Component\Notifier\Message\ChatMessage;
use Symfony\Component\Notifier\Message\MessageInterface;
use Symfony\Component\Notifier\Message\SmsMessage;
use Symfony\Component\Notifier\Test\TransportTestCase;
use Symfony\Contracts\HttpClient\HttpClientInterface;

class TwitterTransportTest extends TransportTestCase
{
public function createTransport(HttpClientInterface $client = null): TwitterTransport
{
return new TwitterTransport('APIK', 'APIS', 'TOKEN', 'SECRET', $client ?? $this->createMock(HttpClientInterface::class));
}

public function toStringProvider(): iterable
{
yield ['twitter://api.twitter.com', $this->createTransport()];
}

public function supportedMessagesProvider(): iterable
{
yield [new ChatMessage('Hello!')];
}

public function unsupportedMessagesProvider(): iterable
{
yield [new SmsMessage('0611223344', 'Hello!')];
yield [$this->createMock(MessageInterface::class)];
}

public function testBasicTweet()
{
$transport = $this->createTransport(new MockHttpClient(function (string $method, string $url, array $options) {
$this->assertSame('POST', $method);
$this->assertSame('https://api.twitter.com/2/tweets', $url);
$this->assertSame('{"text":"Hello World!"}', $options['body']);
$this->assertArrayHasKey('authorization', $options['normalized_headers']);

return new MockResponse('{"data":{"id":"abc123"}}');
}));

$result = $transport->send(new ChatMessage('Hello World!'));

$this->assertSame('abc123', $result->getMessageId());
}

public function testTweetImage()
{
$transport = $this->createTransport(new MockHttpClient((function () {
yield function (string $method, string $url, array $options) {
$this->assertSame('POST', $method);
$this->assertSame('https://upload.twitter.com/1.1/media/upload.json?command=INIT&total_bytes=185&media_type=image%2Fgif&media_category=tweet_image', $url);
$this->assertArrayHasKey('authorization', $options['normalized_headers']);

return new MockResponse('{"media_id_string":"gif123"}');
};

yield function (string $method, string $url, array $options) {
$this->assertSame('POST', $method);
$this->assertSame('https://upload.twitter.com/1.1/media/upload.json?command=APPEND&media_id=gif123&segment_index=0', $url);
$this->assertArrayHasKey('authorization', $options['normalized_headers']);

return new MockResponse();
};

yield function (string $method, string $url, array $options) {
$this->assertSame('POST', $method);
$this->assertSame('https://upload.twitter.com/1.1/media/upload.json?command=FINALIZE&media_id=gif123', $url);
$this->assertArrayHasKey('authorization', $options['normalized_headers']);

return new MockResponse('{"processing_info":{"state":"pending","check_after_secs": 0}}');
};

yield function (string $method, string $url, array $options) {
$this->assertSame('GET', $method);
$this->assertSame('https://upload.twitter.com/1.1/media/upload.json?command=STATUS&media_id=gif123', $url);
$this->assertArrayHasKey('authorization', $options['normalized_headers']);

return new MockResponse('{"processing_info":{"state":"succeeded"}}');
};

yield function (string $method, string $url, array $options) {
$this->assertSame('POST', $method);
$this->assertSame('https://upload.twitter.com/1.1/media/metadata/create.json', $url);
$this->assertSame('{"media_id":"gif123","alt_text":{"text":"A fixture"}}', $options['body']);
$this->assertArrayHasKey('authorization', $options['normalized_headers']);

return new MockResponse('{"processing_info":{"state":"succeeded"}}');
};

yield function (string $method, string $url, array $options) {
$this->assertSame('POST', $method);
$this->assertSame('https://api.twitter.com/2/tweets', $url);
$this->assertSame('{"text":"Hello World!","media":{"media_ids":["gif123"]}}', $options['body']);
$this->assertArrayHasKey('authorization', $options['normalized_headers']);

return new MockResponse('{"data":{"id":"abc123"}}');
};
})()));

$result = $transport->send(new ChatMessage('Hello World!', (new TwitterOptions())
->attachImage(new File(__DIR__.'/fixtures.gif'), 'A fixture'))
);

$this->assertSame('abc123', $result->getMessageId());
}

public function testTweetVideo()
{
$transport = $this->createTransport(new MockHttpClient((function () {
yield function (string $method, string $url, array $options) {
$this->assertSame('POST', $method);
$this->assertSame('https://upload.twitter.com/1.1/media/upload.json?command=INIT&total_bytes=185&media_type=image%2Fgif&media_category=tweet_video', $url);
$this->assertArrayHasKey('authorization', $options['normalized_headers']);

return new MockResponse('{"media_id_string":"gif123"}');
};

yield function (string $method, string $url, array $options) {
$this->assertSame('POST', $method);
$this->assertSame('https://upload.twitter.com/1.1/media/upload.json?command=INIT&total_bytes=185&media_type=image%2Fgif&media_category=subtitles', $url);
$this->assertArrayHasKey('authorization', $options['normalized_headers']);

return new MockResponse('{"media_id_string":"sub234"}');
};

yield function (string $method, string $url, array $options) {
$this->assertSame('POST', $method);
$this->assertSame('https://upload.twitter.com/1.1/media/upload.json?command=APPEND&media_id=gif123&segment_index=0', $url);
$this->assertArrayHasKey('authorization', $options['normalized_headers']);

return new MockResponse();
};

yield function (string $method, string $url, array $options) {
$this->assertSame('POST', $method);
$this->assertSame('https://upload.twitter.com/1.1/media/upload.json?command=APPEND&media_id=sub234&segment_index=0', $url);
$this->assertArrayHasKey('authorization', $options['normalized_headers']);

return new MockResponse();
};

yield function (string $method, string $url, array $options) {
$this->assertSame('POST', $method);
$this->assertSame('https://upload.twitter.com/1.1/media/upload.json?command=FINALIZE&media_id=gif123', $url);
$this->assertArrayHasKey('authorization', $options['normalized_headers']);

return new MockResponse('{}');
};

yield function (string $method, string $url, array $options) {
$this->assertSame('POST', $method);
$this->assertSame('https://upload.twitter.com/1.1/media/upload.json?command=FINALIZE&media_id=sub234', $url);
$this->assertArrayHasKey('authorization', $options['normalized_headers']);

return new MockResponse('{}');
};

yield function (string $method, string $url, array $options) {
$this->assertSame('POST', $method);
$this->assertSame('https://upload.twitter.com/1.1/media/subtitles/create.json', $url);
$this->assertSame('{"media_id":"gif123","media_category":"tweet_video","subtitle_info":{"subtitles":[{"media_id":"sub234","language_code":"en","display_name":"English"}]}}', $options['body']);
$this->assertArrayHasKey('authorization', $options['normalized_headers']);

return new MockResponse();
};

yield function (string $method, string $url, array $options) {
$this->assertSame('POST', $method);
$this->assertSame('https://api.twitter.com/2/tweets', $url);
$this->assertSame('{"text":"Hello World!","media":{"media_ids":["gif123"]}}', $options['body']);
$this->assertArrayHasKey('authorization', $options['normalized_headers']);

return new MockResponse('{"data":{"id":"abc123"}}');
};
})()));

$result = $transport->send(new ChatMessage('Hello World!', (new TwitterOptions())
->attachVideo(new File(__DIR__.'/fixtures.gif'), '', new File(__DIR__.'/fixtures.gif', 'English.en.srt')))
);

$this->assertSame('abc123', $result->getMessageId());
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading