Skip to content

Instantly share code, notes, and snippets.

@rousan
Last active March 2, 2020 10:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rousan/0648ea55a6e56292adeabbae85aa37a4 to your computer and use it in GitHub Desktop.
Save rousan/0648ea55a6e56292adeabbae85aa37a4 to your computer and use it in GitHub Desktop.
Scripts to import/export slack messages from/to a conversation or a channel
const fs = require("fs-extra");
const axios = require("axios");
const webhookUrl = "webhook_url_for_the_destination_conversation_or_channel_of_the_app_created_on_the_destination_slack_Workspace";
(async function () {
const messages = fs.readJSONSync('./messages.json');
for (const m of messages) {
// const { text } = m;
// if (!text) {
// continue;
// }
// await axios.post(webhookUrl, { text });
const { blocks } = m;
await axios.post(webhookUrl, { attachments: [{ blocks }] });
}
})().then(() => {
console.log("Success");
})
.catch((err) => {
console.error(err.messages);
})
const fs = require("fs-extra");
const axios = require("axios");
let cursor;
const limit = 1000;
const messages = [];
(async function () {
while (1) {
const { data } = await axios.get("https://slack.com/api/conversations.history", {
params: {
token: "<Access_token_for_an_app_created_on_the_source_slack_workspace_with_following_permissions>",
channel: "<channle_id_or_conversation_id_i.e._2nd_part_on_the_url_https://app.slack.com/client/T0G78QW8L/D71PCNZLY>",
cursor,
inclusive: 1,
limit,
oldest: 0,
pretty: 0
}
});
if (!data.ok) {
throw new Error(`Error: ${data}`);
}
messages.push(...data.messages);
if (data.has_more) {
cursor = data.response_metadata.next_cursor;
} else {
return;
}
}
})().then(() => {
// const fMessages = messages.filter((m) => {
// if (m.subtype !== "bot_message") {
// return false;
// }
// const { blocks } = m;
// if (!blocks) {
// return false;
// }
// const dumbText = JSON.stringify(blocks).toLowerCase();
// if (dumbText.includes("some_filters")) {
// return false;
// }
// return true;
// });
const fMessages = messages;
fs.writeJSONSync("./messages.json", fMessages.reverse(), { spaces: 2 });
})
.catch((err) => {
console.error(err);
});
/*
* Permissions for the app created on the source slack workspace
* channels:history
* groups:history
* im:history
* mpim:history
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment