Skip to content

Commit

Permalink
feat(telegram/vonage/twilio): fixed and tested telegram with queue work
Browse files Browse the repository at this point in the history
  • Loading branch information
Shadow243 committed Dec 9, 2024
1 parent c58dd48 commit 9faa6f5
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 53 deletions.
4 changes: 1 addition & 3 deletions services/Commands/Hm_CheckMailCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$this->info("Checking for new mail...");
$message = "Hello! You have a new mail on test@entreprise server.";

// $notification = (new Hm_NewMailNotification())
// ->greeting('New Mail Alert')
// ->line($message);
// $notification = new Hm_NewMailNotification($message);
// $notification->sendNow();
Hm_NewMailNotification::dispatch($message);
// Hm_ProcessNewEmail::dispatch(email: '[email protected]');
Expand Down
42 changes: 19 additions & 23 deletions services/Core/Notifications/Channels/Hm_TelegramChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
namespace Services\Core\Notifications\Channels;

use Services\Core\Hm_Container;
use Symfony\Component\Notifier\Notifier;
use Symfony\Component\Notifier\Chatter;
use Symfony\Component\Notifier\Transport\Dsn;
use Symfony\Component\Notifier\Message\ChatMessage;
use Symfony\Component\Notifier\Bridge\Telegram\TelegramOptions;
use Symfony\Component\Notifier\Bridge\Telegram\TelegramTransport;
use Symfony\Component\Notifier\Bridge\Telegram\TelegramTransportFactory;

/**
* Class Hm_TelegramChannel
Expand All @@ -15,11 +15,11 @@
class Hm_TelegramChannel extends Hm_NotificationChannel
{
/**
* The Notifier instance.
* The Chatter instance.
*
* @var Notifier
* @var Chatter
*/
private $notifier;
private $chatter;

/**
* Constructor.
Expand All @@ -29,29 +29,25 @@ public function __construct()
{
$config = Hm_Container::getContainer()->get('config');
$telegramConfig = $config->get('telegram');
$telegramBotToken = $telegramConfig['bot_token'];
$telegramChatId = $telegramConfig['chat_id'];
$telegramTransport = new TelegramTransport($telegramBotToken, $telegramChatId);
$this->notifier = new Notifier([$telegramTransport]);
$telegramToken = $telegramConfig['bot_token'];
$telegramChatId = $telegramConfig['chat_id']; // ID du chat ou username (@username)

$dsnString = sprintf('telegram://%s@default?channel=%s', $telegramToken, $telegramChatId);
$dsn = new Dsn($dsnString);
$factory = new TelegramTransportFactory();
$transport = $factory->create($dsn);
$this->chatter = new Chatter($transport);
}

/**
* Send a Telegram message using the Telegram Bot.
* Send a Telegram message.
*
* @param Hm_Notification $notification The notification object.
*/
public function send($notification): void
{
$telegramMessage = new ChatMessage($notification->getMessageText());

// Optionally add more Telegram message options (like parsing mode)
$telegramMessage->options(
(new TelegramOptions())
->parseMode(TelegramOptions::PARSE_MODE_MARKDOWN_V2)
);

$this->notifier->send($telegramMessage->getNotification());

echo "Message sent via Telegram!";
$chatMessage = new ChatMessage($notification->getContent());
$this->chatter->send($chatMessage);
echo "Message sent to Telegram!";
}
}
}
38 changes: 28 additions & 10 deletions services/Core/Notifications/Channels/Hm_TwilioChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
namespace Services\Core\Notifications\Channels;

use Services\Core\Hm_Container;
use Symfony\Component\Notifier\Notifier;
use Symfony\Component\Notifier\Chatter;
use Symfony\Component\Notifier\Transport\Dsn;
use Symfony\Component\Notifier\Message\SmsMessage;
use Symfony\Component\Notifier\Bridge\Twilio\TwilioTransport;
use Symfony\Component\Notifier\Bridge\Twilio\TwilioTransportFactory;

/**
* Class Hm_TwilioChannel
Expand All @@ -14,11 +15,11 @@
class Hm_TwilioChannel extends Hm_NotificationChannel
{
/**
* The Notifier instance.
* The Chatter instance.
*
* @var Notifier
* @var Chatter
*/
private $notifier;
private $chatter;

/**
* Constructor.
Expand All @@ -28,8 +29,19 @@ public function __construct()
{
$config = Hm_Container::getContainer()->get('config');
$twilioConfig = $config->get('twilio');
$twilioTransport = new TwilioTransport($twilioConfig['sid'], $twilioConfig['token'], $twilioConfig['from']);
$this->notifier = new Notifier([$twilioTransport]);
$twilioAccountSid = $twilioConfig['sid'];
$twilioAuthToken = $twilioConfig['token'];
$twilioFrom = $twilioConfig['from'];
$dsnString = sprintf(
'twilio://%s:%s@default?from=%s',
$twilioAccountSid,
$twilioAuthToken,
$twilioFrom
);
$dsn = new Dsn($dsnString);
$factory = new TwilioTransportFactory();
$transport = $factory->create($dsn);
$this->chatter = new Chatter($transport);
}

/**
Expand All @@ -39,12 +51,18 @@ public function __construct()
* @param string $message The message content.
* @param string $title The title or subject of the notification (optional).
*/
/**
* Send a Twilio SMS message.
*
* @param Hm_Notification $notification The notification object.
*/
public function send($notification): void
{
$smsMessage = new SmsMessage($notification->getRecipient(), $notification->getMessageText());
// Send the message via Twilio
$this->notifier->send($smsMessage->getNotification());
$config = Hm_Container::getContainer()->get('config');
$twilioTo = $config->get('twilio')['to'];

$smsMessage = new SmsMessage($twilioTo, $notification->getContent());
$this->chatter->send($smsMessage);
echo "Message sent via Twilio!";
}
}
44 changes: 28 additions & 16 deletions services/Core/Notifications/Channels/Hm_VonageChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,19 @@
namespace Services\Core\Notifications\Channels;

use Services\Core\Hm_Container;
use Symfony\Component\Notifier\Notifier;
use Symfony\Component\Notifier\Chatter;
use Symfony\Component\Notifier\Transport\Dsn;
use Symfony\Component\Notifier\Message\SmsMessage;
use Symfony\Component\Notifier\Bridge\Vonage\VonageOptions;
use Symfony\Component\Notifier\Bridge\Vonage\VonageTransport;
use Symfony\Component\Notifier\Bridge\Vonage\VonageTransportFactory;

class Hm_VonageChannel extends Hm_NotificationChannel
{
private $notifier;
/**
* The Chatter instance.
*
* @var Chatter
*/
private $chatter;

/**
* Constructor.
Expand All @@ -20,28 +25,35 @@ public function __construct()
{
$config = Hm_Container::getContainer()->get('config');
$vonageConfig = $config->get('vonage');

$vonageApiKey = $vonageConfig['api_key'];
$vonageApiSecret = $vonageConfig['api_secret'];
$vonageFromNumber = $vonageConfig['from'];
$vonageTransport = new VonageTransport($vonageApiKey, $vonageApiSecret, $vonageFromNumber);
$this->notifier = new Notifier([$vonageTransport]);
$vonageFrom = $vonageConfig['from'];

$dsnString = sprintf(
'vonage://%s:%s@default?from=%s',
$vonageApiKey,
$vonageApiSecret,
$vonageFrom
);
$dsn = new Dsn($dsnString);
$factory = new VonageTransportFactory();
$transport = $factory->create($dsn);
$this->chatter = new Chatter($transport);
}

/**
* Send an SMS message using Vonage.
* Send a Vonage SMS message.
*
* @param Hm_Notification $notification The notification object.
*/
public function send($notification): void
{
$smsMessage = new SmsMessage($notification->getRecipient(), $notification->getMessageText());

// Optionally, you can configure options for Vonage (like TTL, etc.)
// $smsMessage->options(new VonageOptions());

// Send the message via Vonage
$this->notifier->send($smsMessage->getNotification());
$config = Hm_Container::getContainer()->get('config');
$vonageTo = $config->get('vonage')['to'];

echo "Message sent via Vonage (Nexmo)!";
$smsMessage = new SmsMessage($vonageTo, $notification->getContent());
$this->chatter->send($smsMessage);
echo "Message sent via Vonage!";
}
}
2 changes: 1 addition & 1 deletion services/Notifications/Hm_NewMailNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ class Hm_NewMailNotification extends Hm_Notification implements Hm_ShouldQueue

public function via(): array
{
return ['slack'];//, 'telegram','broadcast'
return ['telegram'];//, 'slack', 'telegram','broadcast'
}
}

0 comments on commit 9faa6f5

Please sign in to comment.