Skip to content

Commit

Permalink
Telegram notifications (#885)
Browse files Browse the repository at this point in the history
* feat(backend): allow sending notifications to Telegram

* feat(frontend): add inputs for telegram notifications
  • Loading branch information
IgnisDa authored Jun 24, 2024
1 parent d9214a8 commit ff02643
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 1 deletion.
8 changes: 8 additions & 0 deletions apps/backend/src/miscellaneous/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ struct CreateUserNotificationPlatformInput {
#[graphql(secret)]
auth_header: Option<String>,
priority: Option<i32>,
chat_id: Option<String>,
}

#[derive(Enum, Clone, Debug, Copy, PartialEq, Eq)]
Expand Down Expand Up @@ -5383,6 +5384,9 @@ impl MiscellaneousService {
UserNotificationSetting::Email { email } => {
format!("Email: {}", email)
}
UserNotificationSetting::Telegram { chat_id, .. } => {
format!("Telegram Chat ID: {}", chat_id)
}
};
all_notifications.push(GraphqlUserNotificationPlatform {
id: n.id,
Expand Down Expand Up @@ -5472,6 +5476,10 @@ impl MiscellaneousService {
UserNotificationSettingKind::Email => UserNotificationSetting::Email {
email: input.api_token.unwrap(),
},
UserNotificationSettingKind::Telegram => UserNotificationSetting::Telegram {
bot_token: input.api_token.unwrap(),
chat_id: input.chat_id.unwrap(),
},
},
};

Expand Down
15 changes: 15 additions & 0 deletions apps/backend/src/notification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,21 @@ impl UserNotificationSetting {
.unwrap();
mailer.send(&email).map_err(|e| anyhow!(e))?;
}
Self::Telegram { bot_token, chat_id } => {
client
.post(format!(
"https://api.telegram.org/bot{}/sendMessage",
bot_token
))
.json(&serde_json::json!({
"chat_id": chat_id,
"text": msg,
"parse_mode": "Markdown"
}))
.send()
.await
.map_err(|e| anyhow!(e))?;
}
}
Ok(())
}
Expand Down
4 changes: 4 additions & 0 deletions apps/backend/src/users.rs
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,10 @@ pub enum UserNotificationSetting {
Email {
email: String,
},
Telegram {
bot_token: String,
chat_id: String,
},
}

#[derive(Debug, Serialize, Deserialize, Clone, Eq, PartialEq, FromJsonQueryResult)]
Expand Down
11 changes: 11 additions & 0 deletions apps/frontend/app/routes/_dashboard.settings.notifications.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ const deleteSchema = z.object({ notificationId: zx.NumAsString });

const createSchema = z.object({
lot: z.nativeEnum(UserNotificationSettingKind),
chatId: z.string().optional(),
baseUrl: z.string().optional(),
apiToken: z.string().optional(),
authHeader: z.string().optional(),
Expand Down Expand Up @@ -275,6 +276,16 @@ export default function Page() {
<TextInput label="Key" required name="apiToken" />
</>
))
.with(UserNotificationSettingKind.Telegram, () => (
<>
<TextInput
label="Bot Token"
required
name="apiToken"
/>
<TextInput label="Chat ID" required name="chatId" />
</>
))
.with(UserNotificationSettingKind.Email, () => (
<>
<TextInput
Expand Down
4 changes: 3 additions & 1 deletion libs/generated/src/graphql/backend/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ export type CreateUserNotificationPlatformInput = {
apiToken?: InputMaybe<Scalars['String']['input']>;
authHeader?: InputMaybe<Scalars['String']['input']>;
baseUrl?: InputMaybe<Scalars['String']['input']>;
chatId?: InputMaybe<Scalars['String']['input']>;
lot: UserNotificationSettingKind;
priority?: InputMaybe<Scalars['Int']['input']>;
};
Expand Down Expand Up @@ -2161,7 +2162,8 @@ export enum UserNotificationSettingKind {
Ntfy = 'NTFY',
PushBullet = 'PUSH_BULLET',
PushOver = 'PUSH_OVER',
PushSafer = 'PUSH_SAFER'
PushSafer = 'PUSH_SAFER',
Telegram = 'TELEGRAM'
}

export type UserNotificationsPreferences = {
Expand Down

0 comments on commit ff02643

Please sign in to comment.