(ctx: SubscriptionContext)
| 112 | }, |
| 113 | |
| 114 | async createSubscription(ctx: SubscriptionContext): Promise<SubscriptionResult | undefined> { |
| 115 | const config = getProviderConfig(ctx.webhook) |
| 116 | const botToken = config.botToken as string | undefined |
| 117 | |
| 118 | if (!botToken) { |
| 119 | logger.warn(`[${ctx.requestId}] Missing botToken for Telegram webhook ${ctx.webhook.id}`) |
| 120 | throw new Error( |
| 121 | 'Bot token is required to create a Telegram webhook. Please provide a valid Telegram bot token.' |
| 122 | ) |
| 123 | } |
| 124 | |
| 125 | const notificationUrl = getNotificationUrl(ctx.webhook) |
| 126 | const telegramApiUrl = `https://api.telegram.org/bot${botToken}/setWebhook` |
| 127 | |
| 128 | try { |
| 129 | const telegramResponse = await fetch(telegramApiUrl, { |
| 130 | method: 'POST', |
| 131 | headers: { |
| 132 | 'Content-Type': 'application/json', |
| 133 | 'User-Agent': 'TelegramBot/1.0', |
| 134 | }, |
| 135 | body: JSON.stringify({ url: notificationUrl }), |
| 136 | }) |
| 137 | |
| 138 | const responseBody = await telegramResponse.json() |
| 139 | if (!telegramResponse.ok || !responseBody.ok) { |
| 140 | const errorMessage = |
| 141 | responseBody.description || |
| 142 | `Failed to create Telegram webhook. Status: ${telegramResponse.status}` |
| 143 | logger.error(`[${ctx.requestId}] ${errorMessage}`, { response: responseBody }) |
| 144 | |
| 145 | let userFriendlyMessage = 'Failed to create Telegram webhook' |
| 146 | if (telegramResponse.status === 401) { |
| 147 | userFriendlyMessage = |
| 148 | 'Invalid bot token. Please verify that the bot token is correct and try again.' |
| 149 | } else if (responseBody.description) { |
| 150 | userFriendlyMessage = `Telegram error: ${responseBody.description}` |
| 151 | } |
| 152 | |
| 153 | throw new Error(userFriendlyMessage) |
| 154 | } |
| 155 | |
| 156 | logger.info( |
| 157 | `[${ctx.requestId}] Successfully created Telegram webhook for webhook ${ctx.webhook.id}` |
| 158 | ) |
| 159 | return {} |
| 160 | } catch (error: unknown) { |
| 161 | if ( |
| 162 | error instanceof Error && |
| 163 | (error.message.includes('Bot token') || error.message.includes('Telegram error')) |
| 164 | ) { |
| 165 | throw error |
| 166 | } |
| 167 | |
| 168 | logger.error( |
| 169 | `[${ctx.requestId}] Error creating Telegram webhook for webhook ${ctx.webhook.id}`, |
| 170 | error |
| 171 | ) |
nothing calls this directly
no test coverage detected