Skip to content

Commit

Permalink
feat(webex): Handle messages sent to the bot (#281)
Browse files Browse the repository at this point in the history
Refs: #272
  • Loading branch information
damienalexandre authored Nov 16, 2023
1 parent 45292f7 commit 1c59ec0
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/Controller/WebexController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use JoliCode\SecretSanta\Application\WebexApplication;
use JoliCode\SecretSanta\Exception\AuthenticationException;
use JoliCode\SecretSanta\Model\ApplicationToken;
use JoliCode\SecretSanta\Webex\MessageSender;
use JoliCode\SecretSanta\Webex\UserExtractor;
use JoliCode\SecretSanta\Webex\WebexProvider;
use League\OAuth2\Client\Token\AccessToken;
Expand Down Expand Up @@ -105,4 +106,27 @@ public function landing(): Response
{
return $this->render('webex/landing.html.twig');
}

#[Route('/bot/webex', name: 'webex_bot', methods: ['POST'])]
public function bot(Request $request, MessageSender $messageSender): Response
{
$eventData = $request->toArray();

if ('messages' !== $eventData['resource']) {
return new Response();
}

if ('created' !== $eventData['event']) {
return new Response();
}

// Avoid to answer to ourselves
if ('[email protected]' === $eventData['data']['personEmail']) {
return new Response();
}

$messageSender->sendDummyBotAnswer($eventData['data']['personId']);

return new Response();
}
}
14 changes: 14 additions & 0 deletions src/Webex/MessageSender.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,18 @@ public function sendAdminMessage(SecretSanta $secretSanta, string $code, string

throw new MessageSendFailedException($secretSanta, $secretSanta->getConfig()->getAdmin());
}

public function sendDummyBotAnswer(string $string): void
{
$this->client->request('POST', 'https://webexapis.com/v1/messages', [
'auth_bearer' => $this->webexBotToken,
'headers' => [
'accept' => 'application/json',
],
'json' => [
'toPersonId' => $string,
'markdown' => 'Hello, thanks for reaching out! If you wish to run a Secret Santa, you must go to https://secret-santa.team/landing/webex and start over!',
],
]);
}
}
12 changes: 12 additions & 0 deletions templates/content/faq.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,18 @@
<p>Head over our <a href="{{ path('webex_landing') }}">dedicated Webex tutorial</a> to learn more about adding Secret Santa on Webex.</p>
</details>
</li>
<li>
<details id="webex-revoke">
<summary>
<h3>
<p><a href="#webex-revoke"><i class="fas fa-link"></i></a>How do I revoke the permission I gave?</p>
</h3>
</summary>
<p>We are not storing any token or permission - so consider your Webex account already free from all ties with Secret Santa.</p>
<p>Once used, this application will never send you or your contact any message
anymore - it can only do that if you <a href="{{ path('webex_landing') }}">restart the app</a>.</p>
</details>
</li>
</ul>

<h2>Miscellaneous</h2>
Expand Down

0 comments on commit 1c59ec0

Please sign in to comment.