GithubHelp home page GithubHelp logo

alex000kim / slack-gpt-bot Goto Github PK

View Code? Open in Web Editor NEW
75.0 3.0 17.0 366 KB

GPT4-powered Slack bot that can scrape URL contents

License: MIT License

Python 97.66% Dockerfile 2.34%
gpt-4 gpt4 slack slack-bot webscraping chatbot web-scraping

slack-gpt-bot's Introduction

Slack GPT Bot

This repository contains a Python-based Slack GPT Bot that uses OpenAI's GPT model to answer users' questions. Additionally, the bot can extract content from URLs provided in the user's message and take into account their content in its response.

Features

  • Extract URLs from user messages
  • Scrape webpage content from URLs
  • Integrate with OpenAI's GPT-4 to answer questions
  • Maintain conversation context in a threaded format
  • Socket mode integration with Slack

Dependencies

  • Python 3.6 or later
  • beautifulsoup4
  • slack-bolt
  • slack-sdk
  • openai
  • requests

See requirements.txt.

Installation

  1. Clone this repository:
git clone https://github.com/alex000kim/slack-gpt-bot.git
cd slack-gpt-bot
  1. Install the required packages:
pip install -r requirements.txt
  1. Create a .env file in the root directory of the project and add your Slack and OpenAI API keys:
SLACK_BOT_TOKEN=your_slack_bot_token
SLACK_APP_TOKEN=your_slack_app_token
OPENAI_API_KEY=your_openai_api_key

See below how to get those.

Configuring Permissions in Slack

Before you can run the Slack GPT Bot, you need to configure the appropriate permissions for your Slack bot. Follow these steps to set up the necessary permissions:

  1. Create Slack App
  2. Go to your Slack API Dashboard and click on the app you created for this bot.
  3. In the left sidebar, click on "OAuth & Permissions".
  4. In the "Scopes" section, you will find two types of scopes: "Bot Token Scopes" and "User Token Scopes". Add the following scopes under "Bot Token Scopes":
    • app_mentions:read: Allows the bot to read mention events.
    • chat:write: Allows the bot to send messages.
  5. Scroll up to the "OAuth Tokens for Your Workspace" and click "Install App To Workspace" button. This will generate the SLACK_BOT_TOKEN.
  6. In the left sidebar, click on "Socket Mode" and enable it. You'll be prompted to "Generate an app-level token to enable Socket Mode". Generate a token named SLACK_APP_TOKEN and add the connections:write scope.
  7. In the "Features affected" section of "Socket Mode" page, click "Event Subscriptions" and toggle "Enable Events" to "On". Add app_mention event with the app_mentions:read scope in the "Subscribe to bot events" section below the toggle.

Usage

  1. Start the bot:
python slack_gpt_bot.py
  1. Invite the bot to your desired Slack channel.
  2. Mention the bot in a message and ask a question (including any URLs). The bot will respond with an answer, taking into account any extracted content from URLs.

Example

Note: The cutoff date of GPT-4 knowledge is Sep 2021, bit scikit-learn v1.2 was released in Dec 2022 example

slack-gpt-bot's People

Contributors

alex000kim avatar good-lly avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

slack-gpt-bot's Issues

Extracting URL doesn't work if the page is non-html or invalid

@alex000kim If you provide a raw Github README link, it doesn't work since the page is a plain string (extract() doesn't work).
Also, if the URL is invalid, the Slackbot responds "I can't provide a response. Encountered an error: 'NoneType' object has no attribute 'lower'.

I was able to fix these issues by using the following updated code:

def is_html(content):
    content_start = content.lower().strip()[:15]
    return content_start.startswith("<!doctype html>") or content_start.startswith("<html>")

def augment_user_message(user_message, url_list):
    all_url_content = ''
    for url in url_list:
        downloaded = fetch_url(url)
        if downloaded is None:
            return user_message
        # Check if the content is HTML, then use extract() to clean and extract the main text content
        if is_html(downloaded):
            url_content = extract(downloaded, config=newconfig)
        else:
            url_content = downloaded
        user_message = user_message.replace(f'<{url}>', '')
        all_url_content = all_url_content + f' Contents of {url} : \n """ {url_content} """'
    user_message = user_message + "\n" + all_url_content
    return user_message

Please consider applying these changes to your code, or feel free to use a better solution if you know of any. I just wanted to share this with you.
Thank you so much for your great work by the way! :)

Installation is broken due to openai API changes

The application is not working with a fresh install of requirements.txt.

The bot produces this error:

You tried to access openai.ChatCompletion, but this is no longer supported in openai>=1.0.0 - see the README at https://github.com/openai/openai-python for the API.

You can run openai migrate to automatically upgrade your codebase to use the 1.0.0 interface.

Alternatively, you can pin your installation to the old version, e.g. pip install openai==0.28

A detailed migration guide is available here: openai/openai-python#742

To avoid issues like this in future you could pin requirements to concrete versions using pip-compile from pip-tools. This would ensure that the install instructions work for future users even when new packages are released.

.env file is not used

After following the installation steps, I'm getting the errors when running python slack_gpt_bot.py:

Traceback (most recent call last):
  File "/Users/myuser/Documents/GitRepositories/os/slack-gpt-bot/slack_gpt_bot.py", line 10, in <module>
    app = App(token=SLACK_BOT_TOKEN)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/slack_bolt/app/app.py", line 345, in __init__
    self._init_middleware_list(
  File "/opt/homebrew/lib/python3.11/site-packages/slack_bolt/app/app.py", line 397, in _init_middleware_list
    raise BoltError(error_token_required())
slack_bolt.error.BoltError: Either an env variable `SLACK_BOT_TOKEN` or `token` argument in the constructor is required.

When I run with real env variables, it works fine:

SLACK_BOT_TOKEN=... \
SLACK_APP_TOKEN=... \
OPENAI_API_KEY=... \
python slack_gpt_bot.py 

Is something like load_dotenv() missing in the code? I could not make it work myself so asking for support.

Install instructions missing permissions

I followed the instructions to install the bot but after sending it a message it replied with

I can't provide a response. Encountered an error:
The request to the Slack API failed. (url: https://www.slack.com/api/conversations.replies) The server responded with: {'ok': False, 'error': 'missing_scope', 'needed': 'channels:history,groups:history,mpim:history,im:history', 'provided': 'app_mentions:read,chat:write'}

I'm assuming I need to add these scopes.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.