Skip to content

Instantly share code, notes, and snippets.

@mbrock
Last active September 21, 2021 18:21
Show Gist options
  • Save mbrock/818b8361db400808ab3375540d0c607b to your computer and use it in GitHub Desktop.
Save mbrock/818b8361db400808ab3375540d0c607b to your computer and use it in GitHub Desktop.
Roam Telegram integration PoC
// I made this in like one hour while baby was napping.
// It's a proof of concept, but I hope to improve it soon.
// In particular, I really want to grab photos from Telegram.
//
// Make a page in your Roam called [[Telegram Bot]].
//
// Put two nodes in there:
//
// API Key:: <key you get from Telegram's bot system>
// Last Update ID::
//
// Then make a roam/js with this code and put it there.
function updateFromTelegram () {
let apiKeyQuery = roamAlphaAPI.q(`[
:find [?string]
:where
[?page :node/title "Telegram Bot"]
[?block :block/page ?page]
[?block :block/refs ?ref]
[?ref :node/title "API Key"]
[?block :block/string ?string]
]`)
if (!apiKeyQuery.length) {
throw new Error("no API key")
}
let apiKey = apiKeyQuery[0].split(":: ")[1]
let api = `https://api.telegram.org/bot${apiKey}`
let GET = async url => {
let x = await fetch(`${api}/${url}`)
return await x.json()
}
let pull = async () => {
let x = roamAlphaAPI.q(`[
:find (pull ?block [:block/uid :block/string])
:where
[?page :node/title "Telegram Bot"]
[?block :block/page ?page]
[?block :block/refs ?ref]
[?ref :node/title "Latest Update ID"]
[?block :block/string ?string]
]`)
let updateIdBlock
if (x.length) {
updateIdBlock = x[0][0]
} else {
throw new Error("no update ID block")
}
let updateId
if (updateIdBlock.string.match(/:: (\d+)$/)) {
updateId = +RegExp.$1 + 1
} else {
updateId = ""
}
let today = new Date
let yyyy = today.getFullYear()
let mm = (today.getMonth() + 1).toString().padStart(2, '0')
let dd = today.getDate().toString().padStart(2, '0')
let todayUid = `${mm}-${dd}-${yyyy}`
let orders = roamAlphaAPI.q(`[
:find (?order ...)
:where
[?today :block/uid "${todayUid}"]
[?today :block/children ?block]
[?block :block/order ?order]
]`)
let maxOrder = Math.max(...orders)
let updateResponse = await GET(`getUpdates?offset=${updateId}`)
if (updateResponse.result.length) {
let i = 1
for (let { message } of updateResponse.result) {
console.log(message)
let name = message.from.first_name
let date = new Date(1000 * message.date)
let hhmm = date.toLocaleTimeString("en-US", {
hour12: false,
hour: "2-digit",
minute: "2-digit",
})
let text = message.text || ""
text = text.replace(/\bTODO\b/, "{{[[TODO]]}}")
let newuid = roamAlphaAPI.util.generateUID()
roamAlphaAPI.createBlock({
location: { "parent-uid": todayUid, order: maxOrder + i },
block: { uid: newuid, string: `[[${name}]] at ${hhmm}: ${text}` }
})
if (message.photo) {
let fileid = message.photo[message.photo.length - 1].file_id
let photo = await GET(`getFile?chat_id=${message.chat.id}&file_id=${fileid}`)
let url = `https://api.telegram.org/file/bot${apiKey}/${photo.result.file_path}`
roamAlphaAPI.createBlock({
location: { "parent-uid": newuid, order: 0 },
block: { uid: roamAlphaAPI.util.generateUID(), string: `![photo](${url})` }
})
}
i++
}
let lastUpdate = updateResponse.result[updateResponse.result.length - 1]
roamAlphaAPI.updateBlock({
block: {
uid: updateIdBlock.uid,
string: `Latest Update ID:: ${lastUpdate.update_id}`
}
})
}
}
pull()
}
updateFromTelegram()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment