mirror of
https://github.com/amtoaer/bili-sync.git
synced 2026-09-05 23:49:53 +08:00
feat: telegram 通知渠道支持仅发送文字
This commit is contained in:
@@ -9,7 +9,7 @@ use sea_orm::DatabaseConnection;
|
|||||||
use crate::api::wrapper::{ApiError, ApiResponse, ValidatedJson};
|
use crate::api::wrapper::{ApiError, ApiResponse, ValidatedJson};
|
||||||
use crate::bilibili::BiliClient;
|
use crate::bilibili::BiliClient;
|
||||||
use crate::config::{Config, VersionedConfig};
|
use crate::config::{Config, VersionedConfig};
|
||||||
use crate::notifier::Notifier;
|
use crate::notifier::{Message, Notifier};
|
||||||
|
|
||||||
pub(super) fn router() -> Router {
|
pub(super) fn router() -> Router {
|
||||||
Router::new()
|
Router::new()
|
||||||
@@ -41,7 +41,10 @@ pub async fn ping_notifiers(
|
|||||||
*ignore_cache = Some(());
|
*ignore_cache = Some(());
|
||||||
}
|
}
|
||||||
notifier
|
notifier
|
||||||
.notify(bili_client.inner_client(), "This is a test notification from BiliSync.")
|
.notify(bili_client.inner_client(), Message{
|
||||||
|
message: "This is a test notification from BiliSync.".into(),
|
||||||
|
image_url: Some("https://socialify.git.ci/amtoaer/bili-sync/image?description=1&font=KoHo&issues=1&language=1&logo=https%3A%2F%2Fs2.loli.net%2F2023%2F12%2F02%2F9EwT2yInOu1d3zm.png&name=1&owner=1&pattern=Signal&pulls=1&stargazers=1&theme=Light".to_owned()),
|
||||||
|
})
|
||||||
.await?;
|
.await?;
|
||||||
Ok(ApiResponse::ok(()))
|
Ok(ApiResponse::ok(()))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,6 +18,8 @@ pub enum Notifier {
|
|||||||
Telegram {
|
Telegram {
|
||||||
bot_token: String,
|
bot_token: String,
|
||||||
chat_id: String,
|
chat_id: String,
|
||||||
|
#[serde(default)]
|
||||||
|
skip_image: bool,
|
||||||
},
|
},
|
||||||
Webhook {
|
Webhook {
|
||||||
url: String,
|
url: String,
|
||||||
@@ -60,8 +62,14 @@ impl Notifier {
|
|||||||
|
|
||||||
async fn notify_internal<'a>(&self, client: &reqwest::Client, message: &Message<'a>) -> Result<()> {
|
async fn notify_internal<'a>(&self, client: &reqwest::Client, message: &Message<'a>) -> Result<()> {
|
||||||
match self {
|
match self {
|
||||||
Notifier::Telegram { bot_token, chat_id } => {
|
Notifier::Telegram {
|
||||||
if let Some(img_url) = &message.image_url {
|
bot_token,
|
||||||
|
chat_id,
|
||||||
|
skip_image,
|
||||||
|
} => {
|
||||||
|
if let Some(img_url) = &message.image_url
|
||||||
|
&& !*skip_image
|
||||||
|
{
|
||||||
let url = format!("https://api.telegram.org/bot{}/sendPhoto", bot_token);
|
let url = format!("https://api.telegram.org/bot{}/sendPhoto", bot_token);
|
||||||
let params = [
|
let params = [
|
||||||
("chat_id", chat_id.as_str()),
|
("chat_id", chat_id.as_str()),
|
||||||
|
|||||||
@@ -306,6 +306,7 @@ export interface TelegramNotifier {
|
|||||||
type: 'telegram';
|
type: 'telegram';
|
||||||
bot_token: string;
|
bot_token: string;
|
||||||
chat_id: string;
|
chat_id: string;
|
||||||
|
skip_image: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface WebhookNotifier {
|
export interface WebhookNotifier {
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { Button } from '$lib/components/ui/button/index.js';
|
import { Button } from '$lib/components/ui/button/index.js';
|
||||||
|
import { Checkbox } from '$lib/components/ui/checkbox/index.js';
|
||||||
import { Input } from '$lib/components/ui/input/index.js';
|
import { Input } from '$lib/components/ui/input/index.js';
|
||||||
import { Label } from '$lib/components/ui/label/index.js';
|
import { Label } from '$lib/components/ui/label/index.js';
|
||||||
import { toast } from 'svelte-sonner';
|
import { toast } from 'svelte-sonner';
|
||||||
@@ -12,6 +13,7 @@
|
|||||||
let type: 'telegram' | 'webhook' = 'telegram';
|
let type: 'telegram' | 'webhook' = 'telegram';
|
||||||
let botToken = '';
|
let botToken = '';
|
||||||
let chatId = '';
|
let chatId = '';
|
||||||
|
let skipImage = false;
|
||||||
let webhookUrl = '';
|
let webhookUrl = '';
|
||||||
let webhookTemplate = '';
|
let webhookTemplate = '';
|
||||||
let webhookHeaders: { key: string; value: string }[] = [];
|
let webhookHeaders: { key: string; value: string }[] = [];
|
||||||
@@ -23,6 +25,7 @@
|
|||||||
type = 'telegram';
|
type = 'telegram';
|
||||||
botToken = notifier.bot_token;
|
botToken = notifier.bot_token;
|
||||||
chatId = notifier.chat_id;
|
chatId = notifier.chat_id;
|
||||||
|
skipImage = notifier.skip_image;
|
||||||
} else {
|
} else {
|
||||||
type = 'webhook';
|
type = 'webhook';
|
||||||
webhookUrl = notifier.url;
|
webhookUrl = notifier.url;
|
||||||
@@ -37,6 +40,7 @@
|
|||||||
type = 'telegram';
|
type = 'telegram';
|
||||||
botToken = '';
|
botToken = '';
|
||||||
chatId = '';
|
chatId = '';
|
||||||
|
skipImage = false;
|
||||||
webhookUrl = '';
|
webhookUrl = '';
|
||||||
webhookTemplate = '';
|
webhookTemplate = '';
|
||||||
webhookHeaders = [];
|
webhookHeaders = [];
|
||||||
@@ -57,7 +61,8 @@
|
|||||||
const newNotifier: Notifier = {
|
const newNotifier: Notifier = {
|
||||||
type: 'telegram',
|
type: 'telegram',
|
||||||
bot_token: botToken.trim(),
|
bot_token: botToken.trim(),
|
||||||
chat_id: chatId.trim()
|
chat_id: chatId.trim(),
|
||||||
|
skip_image: skipImage
|
||||||
};
|
};
|
||||||
onSave(newNotifier);
|
onSave(newNotifier);
|
||||||
} else {
|
} else {
|
||||||
@@ -121,6 +126,10 @@
|
|||||||
<Input id="chat-id" placeholder="-1001234567890" bind:value={chatId} />
|
<Input id="chat-id" placeholder="-1001234567890" bind:value={chatId} />
|
||||||
<p class="text-muted-foreground text-xs">目标聊天室的 ID(个人用户、群组或频道)</p>
|
<p class="text-muted-foreground text-xs">目标聊天室的 ID(个人用户、群组或频道)</p>
|
||||||
</div>
|
</div>
|
||||||
|
<div class="flex items-center gap-2">
|
||||||
|
<Checkbox id="skip-image" bind:checked={skipImage} />
|
||||||
|
<Label for="skip-image" class="text-sm font-normal">仅发送文字</Label>
|
||||||
|
</div>
|
||||||
{:else if type === 'webhook'}
|
{:else if type === 'webhook'}
|
||||||
<div class="space-y-2">
|
<div class="space-y-2">
|
||||||
<Label for="webhook-url">Webhook URL</Label>
|
<Label for="webhook-url">Webhook URL</Label>
|
||||||
|
|||||||
Reference in New Issue
Block a user