monicahq/monica · error · Exception

Only telegram messages can be sent.

Error message

Only telegram messages can be sent.

What it means

SendTestTelegramNotification sends a test Telegram message on a user notification channel. It loads the channel belonging to the author via findOrFail (unknown id -> 404) and requires type === UserNotificationChannel::TYPE_TELEGRAM ('telegram') before routing the notification via Notification::route('telegram', ...); any other type throws a bare \Exception rendered as a 500.

Source

Thrown at app/Domains/Settings/ManageNotificationChannels/Services/SendTestTelegramNotification.php:63

     */
    public function execute(array $data): UserNotificationChannel
    {
        $this->data = $data;
        $this->validate();
        $this->send();

        return $this->userNotificationChannel;
    }

    private function validate(): void
    {
        $this->validateRules($this->data);

        $this->userNotificationChannel = $this->author->notificationChannels()
            ->findOrFail($this->data['user_notification_channel_id']);

        if ($this->userNotificationChannel->type !== UserNotificationChannel::TYPE_TELEGRAM) {
            throw new Exception('Only telegram messages can be sent.');
        }
    }

    private function send(): void
    {
        $content = trans('This is a test notification for :name', ['name' => $this->author->name]);

        Notification::route('telegram', $this->userNotificationChannel->content)
            ->notify((new ReminderTriggered($this->userNotificationChannel, $content, 'Test'))->locale($this->userNotificationChannel->user->locale));
    }
}

View on GitHub (pinned to e08e917341)

Solutions

  1. Pass the id of a channel whose type is 'telegram'
  2. Reload the notification channels list before triggering the test send
  3. Only show the 'send test telegram' action for telegram channels in the UI
  4. Catch the exception at the call site and return 422 with the message instead of a 500

Example fix

// before
app(SendTestTelegramNotification::class)->execute([
    'user_notification_channel_id' => $channelId, // might be an email channel
]);

// after
$channel = $author->notificationChannels()->findOrFail($channelId);
abort_unless($channel->type === UserNotificationChannel::TYPE_TELEGRAM, 422, 'Only telegram messages can be sent.');
app(SendTestTelegramNotification::class)->execute([
    'user_notification_channel_id' => $channel->id,
]);
Defensive patterns

Strategy: validation

Validate before calling

// Validate before calling: the channel must exist and be a telegram channel
$channel = $author->notificationChannels()->find($data['user_notification_channel_id']);

if ($channel === null) {
    throw ValidationException::withMessages(['user_notification_channel_id' => 'Channel not found.']);
}
if ($channel->type !== UserNotificationChannel::TYPE_TELEGRAM) {
    throw ValidationException::withMessages(['user_notification_channel_id' => 'Only telegram channels can receive a test message.']);
}

Type guard

function isTelegramChannel(?UserNotificationChannel $channel): bool
{
    return $channel !== null
        && $channel->type === UserNotificationChannel::TYPE_TELEGRAM;
}

Try / catch

try {
    app(SendTestTelegramNotification::class)->execute($data);
} catch (\Exception $e) {
    if ($e->getMessage() === 'Only telegram messages can be sent.') {
        // wrong channel type: surface as 422 and refresh the channel list
        throw ValidationException::withMessages(['user_notification_channel_id' => $e->getMessage()]);
    }
    throw $e;
}

Prevention

When it happens

Trigger: Invoking the 'send test telegram notification' action with the user_notification_channel_id of an email channel — e.g. a stale UI selection or an API consumer passing the id of the wrong channel row.

Common situations: UI channel list not refreshed after adding/editing channels, ids swapped between the email and telegram forms, or scripts iterating all channels and calling the telegram test on each.

Related errors


AI-assisted analysis of monicahq/monica@e08e917341 (2026-08-17). Data as JSON: /api/errors/c286a029cd9d226d. Report an issue: GitHub.