Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add GitHub Actions workflow and poll_and_respond script for Slack Cha… #104

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions .github/workflows/on_demand_chatgpt_bot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Trigger ChatGPT Slack Bot

on:
workflow_dispatch: # Enables manual triggering from GitHub Actions

jobs:
run-bot:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Set up Node.js
uses: actions/setup-node@v2
with:
node-version: '16'

- name: Install dependencies
run: npm install

- name: Run ChatGPT Slack Bot
env:
SLACK_BOT_TOKEN: ${{ secrets.SLACK_BOT_TOKEN }}
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
run: |
node scripts/poll_and_respond.js
41 changes: 41 additions & 0 deletions scripts/poll_and_respond.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const { WebClient } = require('@slack/web-api');
const axios = require('axios');

const slackToken = process.env.SLACK_BOT_TOKEN;
const openAiKey = process.env.OPENAI_API_KEY;
const slackClient = new WebClient(slackToken);

(async () => {
try {
// Fetch recent messages mentioning the bot
const result = await slackClient.conversations.history({
channel: 'YOUR_CHANNEL_ID', // Replace with the channel ID you want to poll
limit: 10,
});

for (const message of result.messages) {
if (message.text.includes('<@YOUR_BOT_USER_ID>')) { // Replace with your bot's user ID
// Send message text to ChatGPT for a response
const chatGptResponse = await axios.post(
'https://api.openai.com/v1/completions',
{
model: 'text-davinci-003',
prompt: message.text,
max_tokens: 50,
},
{
headers: { Authorization: `Bearer ${openAiKey}` },
}
);

// Post ChatGPT's response back to Slack
await slackClient.chat.postMessage({
channel: message.channel,
text: chatGptResponse.data.choices[0].text.trim(),
});
}
}
} catch (error) {
console.error('Error running bot:', error);
}
})();
Loading