先上效果图
不局限在云湖,任何有收发消息API的即时通讯软件都可以部署
在 wrangler.toml
里写入如下变量
[vars]
YH_BOT_TOKEN = "换成你的云湖机器人Token"
YH_USER_ID = "换成你的ID"
然后编写代码:
import PostalMime from 'postal-mime';
async function toYunhu(msg, env) {
const token = env.YH_BOT_TOKEN
const apiUrl = 'https://chat-go.jwzhd.com/open-apis/v1/bot/send?token='+token
const response = await fetch(apiUrl, {method: "POST",
headers: {"Content-Type": "application/json; charset=utf-8"},
body: JSON.stringify({
recvId: env.YH_USER_ID,recvType: 'user',
contentType: 'html',content: {text: msg}})})
return response.json()
}
export default {
async email(message, env, ctx) {
let email_string = ``;
const email_from = message.from;
const email_to = message.to;
email_string += `<ul><li>From: <code>${email_from}</code></li><li>To: <code>${email_to}</code></li>`;
const email_content = await PostalMime.parse(message.raw);
const email_subject = email_content.subject;
let email_text = email_content.html;
if (email_text == null) {
email_text = email_content.text;
}
email_string += `<li>Title: <code>${email_subject}</code></li></ul><hr>${email_text}`;
await toYunhu(email_string, env);
},
};
最后使用wrangler部署
打开云湖控制台并新建自定义输入指令
把每个表单ID对应的输入框名记下来!
打开cloudflare网页编辑器并编写代码
async function sendEmail(email_to, email_title, email_content, env) {
const token = env.RESEND_EMAIL_TOKEN
const response = await fetch("https://api.resend.com/emails", {
method: "POST",
headers: {
"content-type": "application/json",
"Authorization": `Bearer ${token}`
},
body: JSON.stringify({
from: "这里填入你的resend邮箱名,如果将域名解析到resend则可以填域名邮箱",
to: email_to,
subject: email_title,
html: email_content
})
});
return response.body;
}
async function toYunhu(msg, env) {
const token = env.YH_BOT_TOKEN
const apiUrl = 'https://chat-go.jwzhd.com/open-apis/v1/bot/send?token='+token
const response = await fetch(apiUrl, {method: "POST",
headers: {"Content-Type": "application/json; charset=utf-8"},
body: JSON.stringify({
recvId: env.YH_USER_ID,recvType: 'user',
contentType: 'html',content: {text: msg}})})
return response.json()
}
async function toHTML(markdown, env) {
const token = env.GH_REST_TOKEN
const response = await fetch("https://api.github.com/markdown", {
method: "POST",
headers: {
'Accept': "application/vnd.github+json",
'User-Agent': "Markdown-To-Html-App",
'Authorization': `Bearer ${token}`,
'X-GitHub-Api-Version': "2022-11-28"
},
body: JSON.stringify({text: markdown}),
})
return response.text();
}
async function getEmailInfo(yhJson) {
let email_info = {}
const formJson = yhJson['event']['message']['content']['formJson']
email_info['address'] = formJson['这里是 目标 输入框的表单ID']['value']
email_info['title'] = formJson['这里是 标题 输入框的表单ID']['value']
email_info['content'] = formJson['这里是 正文 输入框的表单ID']['value']
return email_info
}
export default {
async fetch(request, env, ctx) {
if (request.method == "POST") {
const email_info = await getEmailInfo((await request.json()))
const address = email_info['address']
const title = email_info['title']
const content = await toHTML(email_info['content'], env)
const email_response = await sendEmail(address, title, content, env)
await toYunhu("已发送", env)
return new Response(email_response);
}
},
};
在 worker设置 -> 变量和机密 那里新建变量
部署完成