Skip to content
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

Always reply in existing threads #71

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
18 changes: 12 additions & 6 deletions src/SlackDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,15 @@ private function convertQuestion(Question $question)
*/
public function buildServicePayload($message, $matchingMessage, $additionalParameters = [])
{
if (! Collection::make($matchingMessage->getPayload())->has('team_domain')) {
$matchingMessagePayload = $matchingMessage->getPayload();

// If the matching message is in a thread, reply there
$thread_ts = $matchingMessagePayload->get('thread_ts');
if (! empty($thread_ts)) {
$additionalParameters['thread_ts'] = $thread_ts;
}

if (! Collection::make($matchingMessagePayload)->has('team_domain')) {
$this->resultType = self::RESULT_TOKEN;
$payload = $this->replyWithToken($message, $matchingMessage, $additionalParameters);
} else {
Expand All @@ -263,24 +271,22 @@ public function sendPayload($payload)
}

/**
* @param $message
* @param $message
* @param array $additionalParameters
* @param \BotMan\BotMan\Messages\Incoming\IncomingMessage $matchingMessage
* @return array
*/
public function replyInThread($message, $additionalParameters, $matchingMessage, BotMan $bot)
{
$additionalParameters['thread_ts'] = ! empty($matchingMessage->getPayload()->get('thread_ts'))
? $matchingMessage->getPayload()->get('thread_ts')
: $matchingMessage->getPayload()->get('ts');
$additionalParameters['thread_ts'] = $matchingMessage->getPayload()->get('ts');

$payload = $this->buildServicePayload($message, $matchingMessage, $additionalParameters);

return $bot->sendPayload($payload);
}

/**
* @param $message
* @param $message
* @param array $additionalParameters
* @param \BotMan\BotMan\Messages\Incoming\IncomingMessage $matchingMessage
* @return array
Expand Down
16 changes: 10 additions & 6 deletions src/SlackRTMDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,12 @@ protected function isBot()
*/
public function buildServicePayload($message, $matchingMessage, $additionalParameters = [])
{
// If the matching message is in a thread, reply there
$thread_ts = $matchingMessage->getPayload()->get('thread_ts');
if (! empty($thread_ts)) {
$additionalParameters['thread_ts'] = $thread_ts;
}

$parameters = array_replace_recursive([
'channel' => $matchingMessage->getRecipient() ?: $matchingMessage->getSender(),
'as_user' => true,
Expand All @@ -269,7 +275,7 @@ public function buildServicePayload($message, $matchingMessage, $additionalParam
],
]);

// else check if is a path
// else check if is a path
} elseif ($attachment instanceof BotManFile && file_exists($attachment->getUrl())) {
$this->file = (new File())
->setTitle(basename($attachment->getUrl()))
Expand Down Expand Up @@ -301,16 +307,14 @@ public function sendPayload($payload)
}

/**
* @param $message
* @param $message
* @param array $additionalParameters
* @param IncomingMessage $matchingMessage
* @return SlackRTMDriver
*/
public function replyInThread($message, $additionalParameters, $matchingMessage)
{
$additionalParameters['thread_ts'] = ! empty($matchingMessage->getPayload()->get('thread_ts'))
? $matchingMessage->getPayload()->get('thread_ts')
: $matchingMessage->getPayload()->get('ts');
$additionalParameters['thread_ts'] = $matchingMessage->getPayload()->get('ts');

return $this->reply($message, $matchingMessage, $additionalParameters);
}
Expand Down Expand Up @@ -406,7 +410,7 @@ public function getClient()
/**
* Low-level method to perform driver specific API requests.
*
* @param $endpoint
* @param $endpoint
* @param array $parameters
* @param IncomingMessage $matchingMessage
* @return \React\Promise\PromiseInterface
Expand Down