Last active
March 2, 2023 07:42
-
-
Save kvzhuang/38491df88d681787a60e879b5139cd18 to your computer and use it in GitHub Desktop.
chatgpt-slack-bolt-integration-sample
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Slack App Setting | |
* Enable Socket Mode | |
* Enable Event Subscriptions | |
* OAuth & Permissions: app_mentions:read, chat:write | |
* @slack/bolt requires at least NodeJs version 12.13.0 | |
*/ | |
const { App } = require('@slack/bolt'); | |
const request = require('request'); | |
const OPENAI_API_KEY = process.env.OPENAI_API_KEY; | |
const app = new App({ | |
token: process.env.SLACK_BOT_TOKEN, | |
signingSecret: process.env.SLACK_SIGNING_SECRET, | |
socketMode: true, | |
appToken: process.env.SLACK_APP_TOKEN, | |
// Socket Mode doesn't listen on a port, but in case you want your app to respond to OAuth, | |
// you still need to listen on some port! | |
port: process.env.PORT || 3000 | |
}); | |
function callChatGPT(message){ | |
return new Promise((resolve, reject) => { | |
request.post({ | |
url: 'https://api.openai.com/v1/chat/completions', | |
headers: { | |
'Content-Type': 'application/json', | |
'Authorization': `Bearer ${OPENAI_API_KEY}` | |
}, | |
body: JSON.stringify({ | |
"model": "gpt-3.5-turbo", | |
"messages": [ | |
{ | |
"role": "user", | |
"content": `${message}` | |
}]}), | |
}, (error, response, body) => { | |
if (error) { | |
reject(error); | |
} else { | |
// 將 Chatbot 的回覆訊息發送給用戶 | |
// console.log(body); | |
const jsonBody = JSON.parse(body); | |
//console.log(jsonBody); | |
const answer = jsonBody.choices[0].message?.content; | |
resolve(answer); | |
} | |
}); | |
}); | |
} | |
app.event('app_mention', async ({ event, context, client, say }) => { | |
try { | |
let answer = await callChatGPT(event.text); | |
await say({"blocks": [ | |
{ | |
"type": "section", | |
"text": { | |
"type": "mrkdwn", | |
"text": `<@${event.user}> ${answer}` | |
} | |
} | |
]}); | |
} | |
catch (error) { | |
console.error(error); | |
} | |
}); | |
(async () => { | |
// Start your app | |
await app.start(); | |
console.log('⚡️ Bolt app is running!'); | |
})(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "open-ai-slack-bolt-integration-sample", | |
"version": "1.0.0", | |
"description": "", | |
"main": "app.js", | |
"scripts": { | |
"start": "node app.js" | |
}, | |
"author": "Kevin Zhuang", | |
"license": "ISC", | |
"dependencies": { | |
"@slack/bolt": "^3.12.2", | |
"openai": "^3.1.0", | |
"request": "^2.88.2" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment