{"record":{"id":"c286a029cd9d226d","repo":"monicahq/monica","slug":"only-telegram-messages-can-be-sent","errorCode":null,"errorMessage":"Only telegram messages can be sent.","messagePattern":"Only telegram messages can be sent\\.","errorType":"exception","errorClass":"Exception","httpStatus":500,"severity":"error","filePath":"app/Domains/Settings/ManageNotificationChannels/Services/SendTestTelegramNotification.php","lineNumber":63,"sourceCode":"     */\n    public function execute(array $data): UserNotificationChannel\n    {\n        $this->data = $data;\n        $this->validate();\n        $this->send();\n\n        return $this->userNotificationChannel;\n    }\n\n    private function validate(): void\n    {\n        $this->validateRules($this->data);\n\n        $this->userNotificationChannel = $this->author->notificationChannels()\n            ->findOrFail($this->data['user_notification_channel_id']);\n\n        if ($this->userNotificationChannel->type !== UserNotificationChannel::TYPE_TELEGRAM) {\n            throw new Exception('Only telegram messages can be sent.');\n        }\n    }\n\n    private function send(): void\n    {\n        $content = trans('This is a test notification for :name', ['name' => $this->author->name]);\n\n        Notification::route('telegram', $this->userNotificationChannel->content)\n            ->notify((new ReminderTriggered($this->userNotificationChannel, $content, 'Test'))->locale($this->userNotificationChannel->user->locale));\n    }\n}\n","sourceCodeStart":45,"sourceCodeEnd":75,"githubUrl":"https://github.com/monicahq/monica/blob/e08e91734170b6bbd582cb578532c3948196124e/app/Domains/Settings/ManageNotificationChannels/Services/SendTestTelegramNotification.php#L45-L75","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Pass the id of a channel whose type is 'telegram'","Reload the notification channels list before triggering the test send","Only show the 'send test telegram' action for telegram channels in the UI","Catch the exception at the call site and return 422 with the message instead of a 500"],"exampleFix":"// before\napp(SendTestTelegramNotification::class)->execute([\n    'user_notification_channel_id' => $channelId, // might be an email channel\n]);\n\n// after\n$channel = $author->notificationChannels()->findOrFail($channelId);\nabort_unless($channel->type === UserNotificationChannel::TYPE_TELEGRAM, 422, 'Only telegram messages can be sent.');\napp(SendTestTelegramNotification::class)->execute([\n    'user_notification_channel_id' => $channel->id,\n]);","handlingStrategy":"validation","validationCode":"// Validate before calling: the channel must exist and be a telegram channel\n$channel = $author->notificationChannels()->find($data['user_notification_channel_id']);\n\nif ($channel === null) {\n    throw ValidationException::withMessages(['user_notification_channel_id' => 'Channel not found.']);\n}\nif ($channel->type !== UserNotificationChannel::TYPE_TELEGRAM) {\n    throw ValidationException::withMessages(['user_notification_channel_id' => 'Only telegram channels can receive a test message.']);\n}","typeGuard":"function isTelegramChannel(?UserNotificationChannel $channel): bool\n{\n    return $channel !== null\n        && $channel->type === UserNotificationChannel::TYPE_TELEGRAM;\n}","tryCatchPattern":"try {\n    app(SendTestTelegramNotification::class)->execute($data);\n} catch (\\Exception $e) {\n    if ($e->getMessage() === 'Only telegram messages can be sent.') {\n        // wrong channel type: surface as 422 and refresh the channel list\n        throw ValidationException::withMessages(['user_notification_channel_id' => $e->getMessage()]);\n    }\n    throw $e;\n}","preventionTips":["Refresh the channel list before showing per-channel test actions","Bind test actions to the channel type at render time (telegram button only on telegram rows)","Scope channel selection queries to type where the operation is type-specific","Map this \\Exception to 422 at the boundary so clients never see a 500"],"tags":["monica","notifications","telegram","channel-type"],"backgroundTag":"channel-type-mismatch","analyzedSha":"e08e91734170b6bbd582cb578532c3948196124e","analyzedAt":"2026-08-17T01:36:49.014Z","schemaVersion":2},"datasetVersion":"2026-08-17T04:17:16.089Z"}