新增TG邮件查看HTML
This commit is contained in:
109
mail-worker/src/service/telegram-service.js
Normal file
109
mail-worker/src/service/telegram-service.js
Normal file
@@ -0,0 +1,109 @@
|
||||
import orm from '../entity/orm';
|
||||
import email from '../entity/email';
|
||||
import settingService from './setting-service';
|
||||
import dayjs from 'dayjs';
|
||||
import utc from 'dayjs/plugin/utc';
|
||||
import timezone from 'dayjs/plugin/timezone';
|
||||
dayjs.extend(utc);
|
||||
dayjs.extend(timezone);
|
||||
import { eq } from 'drizzle-orm';
|
||||
import jwtUtils from '../utils/jwt-utils';
|
||||
import emailMsgTemplate from '../template/email-msg';
|
||||
import emailTextTemplate from '../template/email-text';
|
||||
import emailHtmlTemplate from '../template/email-html';
|
||||
import verifyUtils from '../utils/verify-utils';
|
||||
|
||||
const telegramService = {
|
||||
|
||||
async getEmailById(c, params) {
|
||||
|
||||
const { emailId } = params
|
||||
|
||||
const emailRow = await orm(c).select().from(email).where(eq(email.emailId, emailId)).get();
|
||||
|
||||
if (emailRow) {
|
||||
|
||||
if (emailRow.content) {
|
||||
return emailHtmlTemplate(emailRow.content || '')
|
||||
} else {
|
||||
return emailTextTemplate(emailRow.text || '')
|
||||
}
|
||||
|
||||
} else {
|
||||
return emailTextTemplate('The email does not exist')
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
async getEmailContent(c, params) {
|
||||
|
||||
const { token } = params
|
||||
|
||||
const result = await jwtUtils.verifyToken(c, token);
|
||||
|
||||
if (!result) {
|
||||
return emailTextTemplate('Access denied')
|
||||
}
|
||||
|
||||
const emailRow = await orm(c).select().from(email).where(eq(email.emailId, result.emailId)).get();
|
||||
|
||||
if (emailRow) {
|
||||
|
||||
if (emailRow.content) {
|
||||
return emailHtmlTemplate(emailRow.content || '')
|
||||
} else {
|
||||
return emailTextTemplate(emailRow.text || '')
|
||||
}
|
||||
|
||||
} else {
|
||||
return emailTextTemplate('The email does not exist')
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
async sendEmailToBot(c, email) {
|
||||
|
||||
const { tgBotToken, tgChatId, customDomain, tgMsgTo, tgMsgFrom } = await settingService.query(c);
|
||||
|
||||
const tgChatIds = tgChatId.split(',');
|
||||
|
||||
const jwtToken = await jwtUtils.generateToken(c, { emailId: email.emailId })
|
||||
|
||||
const webAppUrl = verifyUtils.isDomain(customDomain) ? `https://${customDomain}/api/telegram/getEmail/${jwtToken}` : 'https://www.cloudflare.com/404'
|
||||
|
||||
await Promise.all(tgChatIds.map(async chatId => {
|
||||
try {
|
||||
const res = await fetch(`https://api.telegram.org/bot${tgBotToken}/sendMessage`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify({
|
||||
chat_id: chatId,
|
||||
parse_mode: 'HTML',
|
||||
text: emailMsgTemplate(email, tgMsgTo, tgMsgFrom),
|
||||
reply_markup: {
|
||||
inline_keyboard: [
|
||||
[
|
||||
{
|
||||
text: '查看',
|
||||
web_app: { url: webAppUrl }
|
||||
}
|
||||
]
|
||||
]
|
||||
}
|
||||
})
|
||||
});
|
||||
if (!res.ok) {
|
||||
console.error(`转发 Telegram 失败: chatId=${chatId}, 状态码=${res.status}`);
|
||||
}
|
||||
} catch (e) {
|
||||
console.error(`转发 Telegram 失败: chatId=${chatId}`, e.message);
|
||||
}
|
||||
}));
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export default telegramService
|
||||
Reference in New Issue
Block a user