Skip to content

Instantly share code, notes, and snippets.

@JamesHopbourn
Created November 26, 2023 07:08
Show Gist options
  • Save JamesHopbourn/24764e090f2c92fbe61c6dc57d566756 to your computer and use it in GitHub Desktop.
Save JamesHopbourn/24764e090f2c92fbe61c6dc57d566756 to your computer and use it in GitHub Desktop.
Telegram Bot 添加 Todoist 任务
addEventListener('fetch', (event) => {
event.respondWith(handleRequest(event.request));
});
const TodoistToken = "";
const TelegramChannelLink = 'https://t.me/c/xxxxxxxxxx'
async function handleRequest(request) {
try {
const data = await request.json();
const channel_post = data.channel_post || {};
const message = data.message || {};
const text = channel_post.caption || channel_post.text || message.text || '';
const message_id = channel_post.message_id || message.message_id || '';
if (!text.includes('#todo')) {
return new Response(JSON.stringify(data), { status: 200 });
}
const todoData = {
"content": text.replace('#todo ', ''),
"description": `${TelegramChannelLink}/${message_id}`
};
const response = await fetch('https://api.todoist.com/rest/v2/tasks', {
method: 'POST',
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${TodoistToken}`
},
body: JSON.stringify(todoData)
});
const responseBody = await response.text();
console.log(responseBody);
return new Response(JSON.stringify(data), { status: 200 });
} catch (error) {
return new Response(JSON.stringify({ error: error.message }), { status: 500 });
}
}
@JamesHopbourn
Copy link
Author

JamesHopbourn commented Nov 26, 2023

token='bot123456789:ABCDE-ABCDEFGHIJKLMNOPQRSTUVWXYZ'

http -b POST "https://api.telegram.org/$token/setWebhook?url={CloudflareWorker地址}"
{
    "description": "Webhook was set",
    "ok": true,
    "result": true
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment