Differential D10168 Diff 24459 src/infrastructure/daemon/bot/adapter/PhabricatorBotFlowdockProtocolAdapter.php
Changeset View
Changeset View
Standalone View
Standalone View
src/infrastructure/daemon/bot/adapter/PhabricatorBotFlowdockProtocolAdapter.php
<?php | <?php | ||||
final class PhabricatorBotFlowdockProtocolAdapter | final class PhabricatorBotFlowdockProtocolAdapter | ||||
extends PhabricatorBotBaseStreamingProtocolAdapter { | extends PhabricatorBotBaseStreamingProtocolAdapter { | ||||
public function getServiceType() { | public function getServiceType() { | ||||
return 'Flowdock'; | return 'Flowdock'; | ||||
} | } | ||||
protected function buildStreamingUrl($channel) { | protected function buildStreamingUrl($channel) { | ||||
$organization = $this->getConfig('organization'); | $organization = $this->getConfig('flowdock.organization'); | ||||
if (empty($organization)) { | |||||
$this->getConfig('organization'); | |||||
} | |||||
if (empty($organization)) { | |||||
throw new Exception( | |||||
'"flowdock.organization" configuration variable not set'); | |||||
} | |||||
$ssl = $this->getConfig('ssl'); | $ssl = $this->getConfig('ssl'); | ||||
$url = ($ssl) ? 'https://' : 'http://'; | $url = ($ssl) ? 'https://' : 'http://'; | ||||
$url .= "stream.flowdock.com/flows/{$organization}/{$channel}"; | $url .= "{$this->authtoken}@stream.flowdock.com/flows/{$organization}/{$channel}"; | ||||
return $url; | return $url; | ||||
} | } | ||||
protected function processMessage($m_obj) { | protected function processMessage($m_obj) { | ||||
$command = null; | $command = null; | ||||
switch ($m_obj['event']) { | switch ($m_obj['event']) { | ||||
case 'message': | case 'message': | ||||
$command = 'MESSAGE'; | $command = 'MESSAGE'; | ||||
break; | break; | ||||
default: | default: | ||||
// For now, ignore anything which we don't otherwise know about. | // For now, ignore anything which we don't otherwise know about. | ||||
break; | break; | ||||
} | } | ||||
if ($command === null) { | if ($command === null) { | ||||
return false; | return false; | ||||
} | } | ||||
// TODO: These should be usernames, not user IDs. | // TODO: These should be usernames, not user IDs. | ||||
$sender = id(new PhabricatorBotUser()) | $sender = id(new PhabricatorBotUser()) | ||||
->setName($m_obj['user']); | ->setName($m_obj['user']); | ||||
$target = id(new PhabricatorBotChannel()) | $target = id(new PhabricatorBotChannel()) | ||||
->setName($m_obj['flow']); | ->setName($m_obj['flow']); | ||||
return id(new PhabricatorBotMessage()) | return id(new PhabricatorBotMessage()) | ||||
->setCommand($command) | ->setCommand($command) | ||||
->setSender($sender) | ->setSender($sender) | ||||
->setTarget($target) | ->setTarget($target) | ||||
->setBody($m_obj['content']); | ->setBody($m_obj['content']); | ||||
} | } | ||||
public function writeMessage(PhabricatorBotMessage $message) { | public function writeMessage(PhabricatorBotMessage $message) { | ||||
switch ($message->getCommand()) { | switch ($message->getCommand()) { | ||||
case 'MESSAGE': | case 'MESSAGE': | ||||
$this->speak( | $this->speak( | ||||
$message->getBody(), | $message->getBody(), | ||||
$message->getTarget()); | $message->getTarget()); | ||||
break; | break; | ||||
} | } | ||||
} | } | ||||
private function speak( | private function speak( | ||||
$body, | $body, | ||||
PhabricatorBotTarget $flow) { | PhabricatorBotTarget $flow) { | ||||
// The $flow->getName() returns the flow's UUID, | |||||
list($organization, $room_id) = explode(':', $flow->getName()); | // as such, the Flowdock API does not require the organization | ||||
// to be specified in the URI | |||||
$this->performPost( | $this->performPost( | ||||
"/flows/{$organization}/{$room_id}/messages", | '/messages', | ||||
array( | array( | ||||
'flow' => $flow->getName(), | |||||
'event' => 'message', | 'event' => 'message', | ||||
'content' => $body)); | 'content' => $body)); | ||||
} | } | ||||
public function __destruct() { | public function __destruct() { | ||||
if ($this->readHandles) { | if ($this->readHandles) { | ||||
foreach ($this->readHandles as $read_handle) { | foreach ($this->readHandles as $read_handle) { | ||||
curl_multi_remove_handle($this->multiHandle, $read_handle); | curl_multi_remove_handle($this->multiHandle, $read_handle); | ||||
curl_close($read_handle); | curl_close($read_handle); | ||||
} | } | ||||
} | } | ||||
curl_multi_close($this->multiHandle); | curl_multi_close($this->multiHandle); | ||||
} | } | ||||
} | } |