Even though there is an official Slack ChatGPT bot in the works, there’s much more joy in DIY. Here’s a few words about how I spent a few hours building our own for our internal company Slack.
My goal was to enhance our existing Slackbot “Loftbot” with ChatGPT like features. Instead of using the web browser in a 1–1 (1 human, 1 robot) fashion, this would make it possible for all Slack users to take part in the chat. At least that was the idea.
As in any programming eco system, there are ready libraries in .NET to do most of the cruft. What I needed:
- Integration with Slack APIs
- Integration with OpenAI APIs
For Slack integration, I needed a webserver hosting an endpoint receiving notifications from Slack. I just picked one I’ve some experience and know works (… disclaimer: I’m the author of it): Slackbot.Net.Endpoints .
There were at least two actively maintained projects that I found for OpenAI clients. I chose a random one providing me what I needed: OpenAI-DotNet.
I won’t go into the details how to create a Slack app, since Slack already has this covered in their docs. For my I use case, I subscribed to app_mention
events and used the HTTP web integration.
I created and configured an ASP.NET Core host to serve an endpoint at /events
, and registered an app_mention
event handler.
The implementation of the event handler is quite simple, but one gotcha from experience working with Slack is to avoid long-running processes in your web-endpoints. Slack will trigger a retry of an event callback if your endpoint does not answer with a 200 within 3 seconds. So, do offload the heavy-lifting to somewhere else.
For my small demo, I just offloaded it to a background thread — fire and forget. Yeah, not perfect, but at least without side effects for my small demo.
And then to the interesting parts: tweaking prompts and providing the bot with context. I started out providing it with some knowledge that it now is a Slackbot with an identity and a name. I also gave it a small table containing all the users in our workspace, since the entire list could fit into the max size token size.
var setup =
$"""
You are a Slackbot named @loftbot in the Slack workspace "Blank".
You provide helpful replies.
Each user in the Slack workspace is employees of a company named "Blank AS".The full list of users are:
| userId | real name | username | bot or human | title | imageurl |
| --- | --- | --- | --- |
{userList}
Reply always in norwegia and use Slack user mentions: '@username'
""";
I guess what the professionals do here, is to rather train a separate model with info instead of always putting a huge payload on every request, but again — this was just for fun. Or maybe the new ChatGPT Plugin system is coming to the APIs, so it can fetch external data on demand here as well? To be continued.
The next task is to provide actual user prompts. I added a small translation between Slack and ChatGPT concepts, and voila — it has enough context to understand Slack threads.
I shipped the .NET app to Heroku, and wired the missing Slack app configuration for it to go live. Without any other tuning of parameters, it started responding to mentions in Slack:
Of course, it did all the ChatGPT things. It made up non-existing employees of our company, gave external links to companies/products that don’t exist, and was very creative about what roles we had in our own company. If not useful, kinda fun. Here I could probably tweak it a bit, making it less “creative” and more deterministic.
To make it a bit more personal to our company, I gave it our internal employee registry (we have an API for it). This enabled queries like:
“Who is the CTO, @loftbot?”
“Give me a list of 5 designers, @loftbot”
“Could @loftbot help me find out who makes up the management team?”
“@loftbot, tell me about <person/employee>” :
- I need a company credit card to pay for usage (hello, Magne!)
- … but it’s not really that expensive. (charges were around $1 USD)
- The developers in our Slack tried to hack it, like — immediately
- With OpenAI, it’s affordable and easy to get something working without a large investment in learning AI / ML, or hardware
No idea. But I’ll make sure that @loftbot is running the decisions.