Template Content
About the template
About ScriptRunner Connect
What is ScriptRunner Connect?
Can I try it out for free?
Yes. ScriptRunner Connect comes with a forever free tier.
Can I customize the integration logic?
Absolutely. The main value proposition of ScriptRunner Connect is that you'll get full access to the code that is powering the integration, which means you can make any changes to the the integration logic yourself.
Can I change the integration to communicate with additional apps?
Yes. Since ScriptRunner Connect specializes in enabling complex integrations, you can easily change the integration logic to connect to as many additional apps as you need, no limitations.
What if I don't feel comfortable making changes to the code?
First you can try out our AI assistant which can help you understand what the code does, and also help you make changes to the code. Alternatively you can hire our professionals to make the changes you need or build new integrations from scratch.
Do I have to host it myself?
No. ScriptRunner Connect is a fully managed SaaS (Software-as-a-Service) product.
What about security?
ScriptRunner Connect is ISO 27001 and SOC 2 certified. Learn more about our security.
This integration allows to ask questions from ChatGPT via Slack slash command.
Parameters
and configure the model version.https://api.openai.com
Authorization
Bearer YOUR_OPENAI_API_KEY
Add features and functionality
-> Bots
-> Show Tabs
, enable Messages Tab
and check Allow users to send Slash commands
/your-slash-command <message to AI>
. You will recieve a response to your Direct Messages to keep your conversations with AI private./your-command NEW CHAT
. This feature will delete the context only for the Slack user who sent this message.import { SlashCommandEvent } from '@sr-connect/slack/events';
import OpenAI from "./api/generic";
import Slack from "./api/slack";
import { RecordStorage } from '@sr-connect/record-storage';
export default async function (event: SlashCommandEvent, context: Context): Promise<void> {
if (context.triggerType === 'MANUAL') {
console.error('This script is designed to be triggered externally or manually from the Event Listener. Please consider using Event Listener Test Event Payload if you need to trigger this script manually.');
return;
}
const MODEL_VERSION = context.environment.vars.MODEL_VERSION;
const storedMessagesContextCache: Message[] = [];
const userId = event.user_id;
const prompt = event.text;
const storage = new RecordStorage();
const storedValues = await storage.getValue<Message[]>(userId);
// Check if prompt was added by the user
if (prompt) {
// Check if the slash command contains actual prompt or a request to start a new chat
if (prompt === 'NEW CHAT') {
await storage.deleteValue(userId);
await Slack.Chat.postMessage({
body: {
channel: userId,
text: 'Previous context cleared.'
}
});
return;
} else {
// If a question was asked, echo it back to Slack
await Slack.Chat.postMessage({
body: {
channel: userId,
text: `*You asked:* ${prompt}`
}
});
}
} else {
// If not, then send appropriate reply
await Slack.Chat.postMessage({
body: {
channel: userId,
text: `No message. Please type your prompt after command: *${event.command} <your message>*.`
}
});
return;
}
const userMessage = { role: "user", content: prompt };
if (!storedValues) {
storedMessagesContextCache.push(userMessage);
} else {
storedMessagesContextCache.push(...storedValues, userMessage)
}
try {
// Send a request to OpenAI
const response = await OpenAI.fetch('/v1/chat/completions', {
method: 'POST',
body: JSON.stringify({
model: MODEL_VERSION,
messages: storedMessagesContextCache,
})
});
// Check if the response is OK
if (!response.ok) {
// If not, then throw an error
throw new Error(`Invalid response while getting GPT response: ${response.status}`)
}
// Extract the response message
const responseMessage = await response.json<OpenAIResponse>();
// Check if the message contains an error
if (responseMessage.error) {
// If so, then throw an error
throw new Error(responseMessage.error.message);
}
// Store the reply in record storage
await storage.setValue(userId, [...storedMessagesContextCache, ...responseMessage.choices.map(ch => ch.message)]);
// Extract the message
const messageOutput = responseMessage.choices.map(ch => ch.message.content).join(' ');
// Finally reply to the user in Slack
await Slack.Chat.postMessage({
body: {
channel: userId,
text: messageOutput
}
});
} catch (e) {
// If something went wrong, then log it out
console.log('Something when wrong while processing AI request', e);
// And reply to user
await Slack.Chat.postMessage({
body: {
channel: userId,
text: `Something went wrong while processing your prompt: ${prompt}`,
}
});
}
}
interface OpenAIResponse {
choices: {
message: Message;
}[];
error?: {
message: string;
}
}
interface Message {
role: string;
content: string;
}