Hi Coda community,
I’m trying to create my first Coda pack for Telegram with those limitations :
- 0 knowledge in JavaScript → I’m using samples from the documentation as starting point.
- ChatGPT help me to write correctly some sentences → but he does not know coda SDK specific syntaxes.
After many hours, I managed to get this Sync Table function working, but I failed to add one last API get call with method getFile from telegram, that use the file_id of the medias generated by the getUpdate API call, to return a file path of the file.
When i put the getfile API get call in the returned results of the getupdate i get an error saying : “#Promise could not be cloned.”
What i’m trying to do is to include files in message inside coda.
i would appreciate if someone can help
import * as coda from "@codahq/packs-sdk";
export const pack = coda.newPack();
pack.setSystemAuthentication({
type: coda.AuthenticationType.Custom,
params: [
{ name: "bot_api_key", description: "The API key" },
],
});
pack.addNetworkDomain("api.telegram.org");
const TaskSchema = coda.makeObjectSchema({
properties: {
message: {
description: "Message.",
type: coda.ValueType.String,
},
from: {
description: "Message sender.",
type: coda.ValueType.String,
},
date: {
description: "Date.",
type: coda.ValueType.Number,
codaType: coda.ValueHintType.Date,
},
caption: {
description: "caption of the media.",
type: coda.ValueType.String,
},
message_id: {
description: "The ID of the message.",
type: coda.ValueType.Number,
},
media: {
description: "The ID of the media.",
type: coda.ValueType.String,
},
},
displayProperty: "message",
idProperty: "message_id",
featuredProperties: ["Message","caption","from","media", "date"],
});
function formatItems(update, username) {
let message;
let media;
if (username) {
if (!update.message.entities) {
return;
}
let entities = update.message.entities;
let isMention = entities.some(function (entity) {
return entity.type === "mention" && update.message.text.substring(entity.offset, entity.offset + entity.length) === username;
});
if (!isMention) {
return;
}
}
if (update.message.photo) {
message = "Picture";
media = update.message.photo[0].file_id;
} else if (update.message.video) {
message = update.message.video.file_name;
media = update.message.video.file_id;
} else if (update.message.document) {
message = update.message.document.file_name;
media = update.message.document.file_id;
} else if (update.message.text) {
message = update.message.text;
}
return {
message: message,
from: update.message.from.first_name,
date: update.message.date,
caption: update.message.caption,
message_id: update.message.message_id,
media: media
};
}
pack.addSyncTable({
name: "Table2",
schema: TaskSchema,
identityName: "MessageTable2",
formula: {
name: "SyncMessages",
description: "Sync from telegram",
parameters: [
coda.makeParameter({
type: coda.ParameterType.Number,
name: "chatID",
description: "The ID of the chat to retrieve messages from",
optional: true,
}),
coda.makeParameter({
type: coda.ParameterType.String,
name: "username",
description: "The username to filter messages by",
optional: true,
}),
],
execute: async function ([chatId = null, username = ""], context) {
let invocationToken = context.invocationToken;
let keyPlaceholder = "{{bot_api_key-" + invocationToken + "}}";
let url = "https://api.telegram.org/bot" + keyPlaceholder + "/getUpdates";
let response = await context.fetcher.fetch({
method: "GET",
url: url,
});
let results = response.body.result;
let items = [];
for (let result of results) {
let formattedItem = formatItems(result, username);
if (formattedItem) {
items.push(formattedItem);
}
}
return {
result: items,
};
},
},
});