Skip to content

Commit f8bf71c

Browse files
committed
Added commands to send chat and team inbox messages
1 parent 725bd3a commit f8bf71c

File tree

8 files changed

+262
-1
lines changed

8 files changed

+262
-1
lines changed

README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,24 @@ if (!$push->sendChatMessage($message, array('connect_timeout' => 1, 'timeout' =>
6868
}
6969
```
7070

71+
You can also do it in your console, look at the help message:
72+
73+
```bash
74+
$ bin/flowdock send-chat-message --help
75+
```
76+
77+
Some arguments are mandatory:
78+
79+
```bash
80+
$ bin/flowdock send-chat-message your_flow_api_token "This message has been sent with mremi/flowdock PHP library" mremi
81+
```
82+
83+
Some options are available:
84+
85+
```bash
86+
$ bin/flowdock send-chat-message your_flow_api_token "This message has been sent with mremi/flowdock PHP library" mremi --message-id=12 --tags="#hello" --tags="#world" --options='{"connect_timeout":1,"timeout":1}'
87+
```
88+
7189
### Team Inbox
7290

7391
```php
@@ -90,6 +108,24 @@ if (!$push->sendTeamInboxMessage($message, array('connect_timeout' => 1, 'timeou
90108
}
91109
```
92110

111+
You can also do it in your console, look at the help message:
112+
113+
```bash
114+
$ bin/flowdock send-team-inbox-message --help
115+
```
116+
117+
Some arguments are mandatory:
118+
119+
```bash
120+
$ bin/flowdock send-team-inbox-message your_flow_api_token source "[email protected]" subject "This message has been sent with mremi/flowdock PHP library"
121+
```
122+
123+
Some options are available:
124+
125+
```bash
126+
$ bin/flowdock send-team-inbox-message your_flow_api_token source "[email protected]" subject "This message has been sent with mremi/flowdock PHP library" --from-name=mremi --reply-to="[email protected]" --project=project --format=html --link="http://www.flowdock.com/" --tags="#hello" --tags="#world" --options='{"connect_timeout":1,"timeout":1}'
127+
```
128+
93129
...and more features coming soon...
94130

95131
<a name="contribution"></a>

bin/flowdock

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/usr/bin/env php
2+
<?php
3+
4+
if (file_exists(__DIR__.'/../vendor/autoload.php')) {
5+
require __DIR__.'/../vendor/autoload.php';
6+
} elseif (file_exists(__DIR__.'/../../../autoload.php')) {
7+
require __DIR__.'/../../../autoload.php';
8+
}
9+
10+
use Symfony\Component\Console\Application;
11+
12+
$application = new Application;
13+
14+
foreach (glob(__DIR__.'/../src/Mremi/Flowdock/Command/*Command.php') as $command) {
15+
$class = sprintf('Mremi\Flowdock\Command\%s', rtrim(basename($command), '.php'));
16+
17+
$application->add(new $class);
18+
}
19+
20+
$application->run();

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
],
1414
"require": {
1515
"php": ">=5.3.3",
16-
"guzzle/guzzle": "~3.7"
16+
"guzzle/guzzle": "~3.7",
17+
"symfony/console": "~2.5"
1718
},
1819
"require-dev":{
1920
"phpunit/phpunit": "~4.1"

src/Mremi/Flowdock/Api/Push/BaseMessage.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,30 @@ public function getContent()
6161
return $this->content;
6262
}
6363

64+
/**
65+
* {@inheritdoc}
66+
*/
67+
public function clearTags()
68+
{
69+
$this->tags = array();
70+
71+
return $this;
72+
}
73+
74+
/**
75+
* {@inheritdoc}
76+
*/
77+
public function setTags(array $tags)
78+
{
79+
$this->clearTags();
80+
81+
foreach ($tags as $tag) {
82+
$this->addTag($tag);
83+
}
84+
85+
return $this;
86+
}
87+
6488
/**
6589
* {@inheritdoc}
6690
*/

src/Mremi/Flowdock/Api/Push/BaseMessageInterface.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,22 @@ public function setContent($content);
4343
*/
4444
public function getContent();
4545

46+
/**
47+
* Removes all tags from the message
48+
*
49+
* @return static
50+
*/
51+
public function clearTags();
52+
53+
/**
54+
* Adds multiple tags to the message
55+
*
56+
* @param array $tags
57+
*
58+
* @return static
59+
*/
60+
public function setTags(array $tags);
61+
4662
/**
4763
* Adds a tag to the message
4864
*
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Mremi\Flowdock library.
5+
*
6+
* (c) Rémi Marseille <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Mremi\Flowdock\Command;
13+
14+
use Mremi\Flowdock\Api\Push\ChatMessage;
15+
use Mremi\Flowdock\Api\Push\Push;
16+
17+
use Symfony\Component\Console\Command\Command;
18+
use Symfony\Component\Console\Input\InputArgument;
19+
use Symfony\Component\Console\Input\InputInterface;
20+
use Symfony\Component\Console\Input\InputOption;
21+
use Symfony\Component\Console\Output\OutputInterface;
22+
23+
/**
24+
* Sends a message to a flow's chat
25+
*
26+
* @author Rémi Marseille <[email protected]>
27+
*/
28+
class SendChatMessageCommand extends Command
29+
{
30+
/**
31+
* {@inheritdoc}
32+
*/
33+
protected function configure()
34+
{
35+
$this
36+
->setName('send-chat-message')
37+
->setDescription('Sends a message to a flow\'s chat')
38+
39+
->addArgument('flow-api-token', InputArgument::REQUIRED, 'The flow API token')
40+
->addArgument('content', InputArgument::REQUIRED, 'The content of the message')
41+
->addArgument('external-user-name', InputArgument::REQUIRED, 'The name of the user sending the message')
42+
43+
->addOption('message-id', null, InputOption::VALUE_REQUIRED, 'The identifier of an another message to comment')
44+
->addOption('tags', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'The message tags')
45+
->addOption('options', null, InputOption::VALUE_REQUIRED, 'An array of options used by request');
46+
}
47+
48+
/**
49+
* {@inheritdoc}
50+
*/
51+
protected function execute(InputInterface $input, OutputInterface $output)
52+
{
53+
$push = new Push($input->getArgument('flow-api-token'));
54+
55+
$message = ChatMessage::create()
56+
->setContent($input->getArgument('content'))
57+
->setExternalUserName($input->getArgument('external-user-name'))
58+
->setTags($input->getOption('tags'))
59+
->setMessageId($input->getOption('message-id'));
60+
61+
$options = $input->getOption('options') ? json_decode($input->getOption('options'), true) : array();
62+
63+
if ($push->sendChatMessage($message, $options)) {
64+
$output->writeln('<info>Success:</info> the message has been sent');
65+
66+
return;
67+
}
68+
69+
$output->writeln(sprintf('<error>Failure:</error> %s', $message->getResponseMessage()));
70+
$output->writeln(var_export($message->getResponseErrors(), true));
71+
}
72+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Mremi\Flowdock library.
5+
*
6+
* (c) Rémi Marseille <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Mremi\Flowdock\Command;
13+
14+
use Mremi\Flowdock\Api\Push\Push;
15+
use Mremi\Flowdock\Api\Push\TeamInboxMessage;
16+
17+
use Symfony\Component\Console\Command\Command;
18+
use Symfony\Component\Console\Input\InputArgument;
19+
use Symfony\Component\Console\Input\InputInterface;
20+
use Symfony\Component\Console\Input\InputOption;
21+
use Symfony\Component\Console\Output\OutputInterface;
22+
23+
/**
24+
* Sends a mail-like message to the team inbox of a flow
25+
*
26+
* @author Rémi Marseille <[email protected]>
27+
*/
28+
class SendTeamInboxMessageCommand extends Command
29+
{
30+
/**
31+
* {@inheritdoc}
32+
*/
33+
protected function configure()
34+
{
35+
$this
36+
->setName('send-team-inbox-message')
37+
->setDescription('Sends a mail-like message to the team inbox of a flow')
38+
39+
->addArgument('flow-api-token', InputArgument::REQUIRED, 'The flow API token')
40+
->addArgument('source', InputArgument::REQUIRED, 'The human readable identifier of the application that uses the Flowdock Push API')
41+
->addArgument('from-address', InputArgument::REQUIRED, 'The email address of the message sender')
42+
->addArgument('subject', InputArgument::REQUIRED, 'The subject line of the message')
43+
->addArgument('content', InputArgument::REQUIRED, 'The content of the message')
44+
45+
->addOption('from-name', null, InputOption::VALUE_REQUIRED, 'The name of the message sender')
46+
->addOption('reply-to', null, InputOption::VALUE_REQUIRED, 'The email address for replies')
47+
->addOption('project', null, InputOption::VALUE_REQUIRED, 'The human readable identifier for more detailed message categorization')
48+
->addOption('format', null, InputOption::VALUE_REQUIRED, 'The format of the message content')
49+
->addOption('link', null, InputOption::VALUE_REQUIRED, 'The link associated with the message')
50+
->addOption('tags', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'The message tags')
51+
->addOption('options', null, InputOption::VALUE_REQUIRED, 'An array of options used by request');
52+
}
53+
54+
/**
55+
* {@inheritdoc}
56+
*/
57+
protected function execute(InputInterface $input, OutputInterface $output)
58+
{
59+
$push = new Push($input->getArgument('flow-api-token'));
60+
61+
$message = TeamInboxMessage::create()
62+
->setSource($input->getArgument('source'))
63+
->setFromAddress($input->getArgument('from-address'))
64+
->setSubject($input->getArgument('subject'))
65+
->setContent($input->getArgument('content'))
66+
->setFromName($input->getOption('from-name'))
67+
->setReplyTo($input->getOption('reply-to'))
68+
->setProject($input->getOption('project'))
69+
->setFormat($input->getOption('format'))
70+
->setLink($input->getOption('link'))
71+
->setTags($input->getOption('tags'));
72+
73+
$options = $input->getOption('options') ? json_decode($input->getOption('options'), true) : array();
74+
75+
if ($push->sendTeamInboxMessage($message, $options)) {
76+
$output->writeln('<info>Success:</info> the message has been sent');
77+
78+
return;
79+
}
80+
81+
$output->writeln(sprintf('<error>Failure:</error> %s', $message->getResponseMessage()));
82+
$output->writeln(var_export($message->getResponseErrors(), true));
83+
}
84+
}

tests/Mremi/Flowdock/Tests/Api/Push/BaseMessageTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,14 @@ public function testAddTags()
7575
$this->message->addTag('#world');
7676
$this->assertCount(2, $this->message->getTags());
7777
$this->assertEquals(array('#hello', '#world'), $this->message->getTags());
78+
79+
$this->message->clearTags();
80+
$this->assertTrue(is_array($this->message->getTags()));
81+
$this->assertCount(0, $this->message->getTags());
82+
83+
$this->message->setTags(array('#foo', '#bar'));
84+
$this->assertCount(2, $this->message->getTags());
85+
$this->assertEquals(array('#foo', '#bar'), $this->message->getTags());
7886
}
7987

8088
/**

0 commit comments

Comments
 (0)