Created
November 26, 2023 07:08
-
-
Save JamesHopbourn/24764e090f2c92fbe61c6dc57d566756 to your computer and use it in GitHub Desktop.
Telegram Bot 添加 Todoist 任务
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); | |
} | |
} |
Author
JamesHopbourn
commented
Nov 26, 2023
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment