Skip to content

Instantly share code, notes, and snippets.

@autonome
Last active July 13, 2024 12:37
Show Gist options
  • Save autonome/96c809b1774651d5bbb2dcf07e38833e to your computer and use it in GitHub Desktop.
Save autonome/96c809b1774651d5bbb2dcf07e38833e to your computer and use it in GitHub Desktop.
/*
Zapier provides the `inputData` global.
This script assumes you've passed in three params:
- text: the post text
- url: a url to attach to the post
- status: `${text} ${url}`
When you add this to your Zap, comment the placeholder data below out.
*/
const text = 'help, i am stuck in a drivers license factory';
const url = 'http://example.com';
const status = `${text} ${url}`;
const inputData = {
text,
url,
status
};
// Script begins here.
// Comment out or remove everything above.
const https = require('node:https');
const host = 'bsky.social';
// in your zap, replace with your actual credentials
const identifier = process.env.BSKY_ID;
const password = process.env.BSKY_PW;
const sessionPath = '/xrpc/com.atproto.server.createSession';
const postPath = '/xrpc/com.atproto.repo.createRecord';
// lil helper
const makeReq = (hostname, path, headers = {}, body = '', cb) => {
const options = {
method: 'POST',
hostname,
path,
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
}
};
Object.assign(options.headers, headers);
const req = https.request(options, (res) => {
const chunks = [];
res.on('data', (chunk) => {
chunks.push(chunk);
});
res.on('end', (chunk) => {
const resp = Buffer.concat(chunks);
const str = resp.toString();
const obj = JSON.parse(str);
cb(obj);
});
res.on('error', (error) => {
console.error(error);
});
});
req.end(JSON.stringify(body));
};
// ok go!
makeReq(host, sessionPath, {}, { identifier, password }, res => {
const Authorization = `Bearer ${res.accessJwt}`;
const postBody = {
repo: res.did,
collection: 'app.bsky.feed.post',
record: {
text: inputData.text,
createdAt: (new Date()).toISOString(),
embed: {
"$type": "app.bsky.embed.external",
external: {
uri: inputData.url,
title: inputData.text,
description: inputData.text
}
}
}
};
makeReq(host, postPath, { Authorization }, postBody, res => {
callback(null, {res})
});
});
@autonome
Copy link
Author

Zapier JS docs with very useful info (that i should've read in advance): https://help.zapier.com/hc/en-us/articles/8496310939021-Use-JavaScript-code-in-Zaps

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