Archiving Slack threads to Discourse with GPT-3
Published: 2022-10-17 17:43:11

---
title: Archiving Slack threads to Discourse with GPT-3
published: true
date: 2022-10-16 22:36:14 UTC
tags: 
canonical_url: https://pipedream.com/blog/archiving-slack-threads-to-discourse-with-gpt-3/
---

 ![Archiving Slack threads to Discourse with GPT-3](https://pipedream.com/blog/content/images/2022/10/Cover-Image.png)

We operate [a public Slack community](https://join.slack.com/t/pipedream-users/shared_invite/zt-11sazmsg0-oS2VW~mZfhtk3XfEEvb~JA) at Pipedream. Our users love the quick, synchronous chat, and the communication happens in public, so it's easy for others to find answers to common questions.

But these messages are private to Slack. You can't search Google for answers — you've got to sign up for our workspace. Plus, Slack deletes messages after 90 days on the free plan, so we lose threads quickly. But upgrading to a paid plan is costly for communities with thousands of users.

So what do we do?

## Discourse as an archive

Discourse provides an open-source discussion forum. We already run [our own Discourse forum](https://pipedream.com/community%20) for people who prefer forums to chat. All posts are public and indexed by search engines.

![Archiving Slack threads to Discourse with GPT-3](https://pipedream.com/blog/content/images/2022/10/Screen-Shot-2022-10-14-at-6.22.59-PM.png)
_Google knows about 858 pages on https://pipedream.com/community_

[Discourse provides an API](https://docs.discourse.org/) that will let us programmatically create new **topics** (the Discourse equivalent of a thread). All we need to do is convert the Slack messages to format Discourse accepts, and we'll have an archive.

For that, we use [Pipedream](https://dev.to/scottw/pipedream-l02-temp-slug-8448815).

## Discourse Bot

When someone asks a question in Slack, we start a [thread](https://slack.com/help/articles/115000769927-Use-threads-to-organize-discussions-) to organize communication on a specific issue. We created a Slack Bot that lets us easily archive these threads.

When we've solved a thread, we mention **@Discourse Bot** with the title we want for the archived Discourse post. For example, for this thread:

![Archiving Slack threads to Discourse with GPT-3](https://pipedream.com/blog/content/images/2022/10/Untitled-1.jpg)

We post:

> [@Discourse Bot](https://pipedream-users.slack.com/team/U01QX068GS0) Deprecating an Action

The "at-mention" runs [this Pipedream workflow](https://pipedream.com/@pd/public-slack-discourse-bot-post-to-discourse-p_mkC5qqk/edit), which creates a Discourse topic with the contents of the Slack thread, adding the title we sent in the message:

![Archiving Slack threads to Discourse with GPT-3](https://pipedream.com/blog/content/images/2022/10/Screen-Shot-2022-10-14-at-6.28.48-PM.png)

[The workflow](https://pipedream.com/@pd/public-slack-discourse-bot-post-to-discourse-p_mkC5qqk/edit) handles a ton of useful logic:

- Fetches all messages in the thread where Discourse Bot was called
- Not every Slack user has a corresponding Discourse account, so we convert Slack usernames to generic Discourse users, like `user-1` and `user-2` above. Not perfect, but it distinguishes messages from different Slack users on the original thread.
- Converts [Slack's `mrkdwn` format](https://api.slack.com/reference/surfaces/formatting) to standard Markdown, which Discourse uses for posts.
- Creates a Discourse topic with posts for each message in the thread. We also create the topic in the right category, e.g. the **`#announcements`** channel archives threads to [Announcements](https://pipedream.com/community/c/announcements/10), **`#help`** is archived to [Help](https://pipedream.com/community/c/help/5), etc.

## Challenges with this approach

Discourse Bot was working great. But it took time to manage this process:

1. It’s hard to keep track of threads that are _complete_. We'd set calendar reminders once a week, find all the threads we solved in the last week, and archive them to Discourse, but we inevitably missed some.
2. It took time to read through some threads and figure out an appropriate summary for the title of the Discourse topic.

We let these problems simmer in the background for a few months until we thought of something that might work.

## Calling threads "done"

We can safely call threads done **when**  **7 days have passed since the last message**. Any time you need to trigger something after a period of inactivity, you can use [a dead man's switch](https://en.wikipedia.org/wiki/Dead_man%27s_switch):

1. Set a timer, e.g. 7 days. This is your switch.
2. Some service pings the switch every so often, checking in. Every time the service checks in, we reset the timer to 7 days.
3. If the service stops checking in, and 7 days pass, the switch activates, triggering some action.

This is a common feature of alerting tools. Your server can ping a monitoring system every minute to tell it the server is up. If the server stops pinging, it might be down, so the monitoring system triggers an alert.

A dead man's switch can trigger any action. In this case, when 7 days have passed since the last message on a thread, we want to trigger a Pipedream workflow.

We built the switch in AWS, using [DynamoDB TTLs](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/TTL.html). DynamoDB has a few properties that make it a perfect option:

- It triggers an event when an item hits its TTL, using [DynamoDB streams](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Streams.html).
- To reset the "timer" for a Slack thread, we overwrite the record for a given (`channel` , `thread_ts` ) — the key which uniquely identifies a thread — when new messages arrive on that thread. [Pipedream workflow #1](https://pipedream.com/new?h=tch_O6Lfz0) handles that logic. Every public Slack message in our workspace triggers the workflow, sending them to Dynamo.
- It's cheap. Assuming an average Slack thread has ten messages, it costs us roughly $0.00001538 to process one thread. That's about $15 for every one million threads, which we're unlikely to hit soon!

When threads expire, they trigger [workflow #2](https://pipedream.com/new?h=tch_OAnfJl). DynamoDB streams can't send an event to a Pipedream workflow directly, so we use a few other AWS services to get that done:

![Archiving Slack threads to Discourse with GPT-3](https://pipedream.com/blog/content/images/2022/10/DynamoDB-Dead-Man-s-Switch.png)

We created [an AWS CDK construct](https://github.com/PipedreamHQ/cdk-constructs) that creates all the necessary resources, so you can easily make your own switch to notify a Pipedream URL when records expire:

```
import { Stack, StackProps } from "aws-cdk-lib";
import { Construct } from "constructs" 
import { DDBDeletedItemsToHTTPS } from "@pipedream/cdk-constructs"

export class DDBDeadMansSwitch extends Stack {
  constructor(scope: Construct, id: string, props?: StackProps) {
    super(scope, id, props);

    new DDBDeletedItemsToHTTPS(this, 'DDBDeletedItemsToHTTPS', {
      notificationURL: "https://your-endpoint.m.pipedream.net",
    });
  }
}
```

## The summarization process

We had also started experimenting with [GPT-3](https://openai.com/api/) internally. GPT-3 is a [language model](https://towardsdatascience.com/the-beginners-guide-to-language-models-aa47165b57f9). You give it a "prompt" — often a question, or a problem to solve — and it produces text that looks like it was written by a human.

GPT-3 can be used for [a variety of tasks](https://beta.openai.com/examples/). For example, we can ask it a question:

> Can I use Pipedream to automate anything?

Thankfully, it replied positively 🤣:

![Archiving Slack threads to Discourse with GPT-3](https://pipedream.com/blog/content/images/2022/10/Screen-Shot-2022-10-14-at-7.00.13-PM.png)

You can ask it to classify items into categories:

![Archiving Slack threads to Discourse with GPT-3](https://pipedream.com/blog/content/images/2022/10/Screen-Shot-2022-10-14-at-7.04.51-PM.png)

You can also use it to summarize text. So we thought we should ask GPT-3 to pick titles for our Discourse topics.

Completed Slack threads from Dynamo trigger [workflow #2](https://pipedream.com/new?h=tch_OAnfJl). That workflow retrieves the thread's messages from the Slack API and asks GPT-3 to summarize it. We experimented with a few prompts. This one yielded the best results:

> Pick a title for the following conversation:

But the summaries can be hit or miss, depending on the thread, so we ask GPT-3 to return its best three summaries from five attempts:

```
{
  "prompt": steps.format_prompt.$return_value,
  "model": "text-davinci-002",
  "max_tokens": 40,
  "n": 3,
  "best_of": 5,
  "temperature": 0.9,
}
```

_GPT-3 model parameters_

Then the workflow sends a private message to our team, asking us to choose the best one:

![Archiving Slack threads to Discourse with GPT-3](https://pipedream.com/blog/content/images/2022/10/Screen-Shot-2022-10-14-at-4.28.21-PM.png)

Choosing a summary triggers [workflow #3](https://pipedream.com/new?h=tch_wEvf2n): it at-mentions **Discourse Bot** on the original thread, which uses [the original Discourse Bot workflow](https://pipedream.com/@pd/public-slack-discourse-bot-post-to-discourse-p_mkC5qqk/edit) to archive the thread. **Bots triggering bots!**

![Archiving Slack threads to Discourse with GPT-3](https://pipedream.com/blog/content/images/2022/10/Screen-Shot-2022-10-14-at-4.28.30-PM.png)

This is what the process looks like end-to-end:

![Archiving Slack threads to Discourse with GPT-3](https://pipedream.com/blog/content/images/2022/10/Slack-Thread-Summarizer--1-.png)
_How Slack, Pipedream and AWS interact_

GPT-3 prices based on [the specific model](https://beta.openai.com/docs/models/finding-the-right-model) you use, and the number of [tokens](https://help.openai.com/en/articles/4936856-what-are-tokens-and-how-to-count-them) you pass it. In our case, threads average ~500 tokens each. Since we're running five summaries per thread, that works out to $.05 to summarize a single thread. Given the SEO benefit of these posts and the time this is saving us, it's a cost we can bear.

## What we learned

GPT-3 is saving us time, and we're finally archiving every solved thread. But the summaries are imperfect, so the process still requires a human in the loop. For our use case, this is acceptable. We'll keep experimenting with new prompts and other methods to improve the model's accuracy.

And while using DynamoDB as a dead man's switch is cool, it's an extra dependency. Building this workflow refined our thinking on how we might add dead man's switches directly into Pipedream.

Are you using GPT-3 or Pipedream for similar use cases? Did this spur any other ideas? [Tell us in our Slack community](https://join.slack.com/t/pipedream-users/shared_invite/zt-11sazmsg0-oS2VW~mZfhtk3XfEEvb~JA)!