Skip to content

An AI-powered Mattermost ChatGPT chatbot that utilizes the OpenAI API to provide helpful, contextual responses to user messages, extract text from links, and describe or generate images. With Docker support!

License

Elehiggle/ChatGPTMattermostChatbot

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

72 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

GitHub Repo stars GitHub Actions Workflow Status Docker Stars Docker Pulls GitHub last commit Codacy Badge GitHub License

ChatGPTMattermostChatbot

Mattermost chat with bot example

This project is a chatbot for Mattermost that integrates with the OpenAI API to provide helpful responses to user messages. The chatbot - like this readme - is mostly written by Claude 3 AI, listens for messages mentioning the chatbot or direct messages, processes the messages, and sends the responses back to the Mattermost channel.

Features

  • Responds to messages mentioning "@chatbot" (or rather the chatbot's username) or direct messages
  • Extracts images, PDFs and other files from message attachments and from URL links in messages
  • Supports FlareSolverr to bypass Javascript/CAPTCHA restrictions
  • Supports DALL-E-3 image generation via function calling
  • Supports the Vision API for describing images. Images from PDFs will also be sent here.
  • Gets transcripts of YouTube videos for easy tl;dw summarizations. Title, description and uploader are also provided
  • Accesses additional live information via function calling. Currently supported: stock data (via Yahoo Finance, eg. ask about AAPL), cryptocurrency data ( via Coingecko), fiat currency exchange rates (via ECB)
  • Maintains context of the conversation within a thread
  • Sends typing indicators to show that the chatbot is processing the message
  • Utilizes a thread pool to handle multiple requests concurrently (due to mattermostdriver-asyncio being outdated)
  • Offers Docker support for easy deployment

Prerequisites

  • Python 3.11 or just a server with Docker (you can get away with using 3.8 if you use datetime.datetime.utcnow() instead of datetime.datetime.now(datetime.UTC))
  • OpenAI API key
  • Mattermost Bot token (alternatively personal access token or login/password for a dedicated Mattermost user account for the chatbot)
  • The bot account needs to be added to the team and to the channels you want it to watch

Installation

  1. Prepare a Mattermost bot account token and OpenAI API key

    • Create an OpenAI API key here
    • Create a Mattermost bot account token directly in the chat: Left menu -> Integrations -> Bot accounts. Give it the post:channels permission (this also suffices for DMs)
  2. Clone the repository:

    git clone https://github.com/Elehiggle/ChatGPTMattermostChatbot.git
    cd ChatGPTMattermostChatbot
  3. Install the required dependencies:

    pip3 install -r requirements.txt

    or alternatively:

    python3.12 -m pip install openai mattermostdriver certifi beautifulsoup4 pillow httpx youtube-transcript-api yt-dlp PyMuPDF defusedxml yfinance
  4. Set the following environment variables with your own values:

Parameter Description
AI_API_KEY Required. Your OpenAI API key
AI_MODEL The OpenAI model to use. Default: "gpt-4o"
MATTERMOST_URL Required. The URL of your Mattermost server
MATTERMOST_TOKEN Required if not using user/password. The bot token (alternatively personal access token) with relevant permissions created specifically for the chatbot. Don't forget to add the bot account to the team
MATTERMOST_USERNAME Required if not using token. The username of the dedicated Mattermost user account for the chatbot (if using username/password login)
MATTERMOST_PASSWORD Required if not using token. The password of the dedicated Mattermost user account for the chatbot (if using username/password login)
MATTERMOST_MFA_TOKEN The MFA token of the dedicated Mattermost user account for the chatbot (if using MFA)

Extended optional configuration variables

Parameter Description
AI_SYSTEM_PROMPT The system prompt/instructions. Default: click (Subject to change. current_time and CHATBOT_USERNAME variables inside the prompt will be auto-formatted and substituted.
AI_TIMEOUT The timeout for the AI API call in seconds. Default: "120"
MAX_TOKENS The maximum number of tokens to generate in the response. Default: "4096" (max)
TEMPERATURE The temperature value for controlling the randomness of the generated responses (0.0 = analytical, 1.0 = fully random). Default: "1"
IMAGE_SIZE The image size for image generation. Default: "1024x1024" (see docs for allowed types)
IMAGE_QUALITY The image quality for image generation. Default: "standard" (also: "hd")
IMAGE_STYLE The image style for image generation. Default: "vivid" (also: "natural")
MAX_RESPONSE_SIZE_MB The maximum size of the website or file content to extract (in megabytes, per URL/file). Default: "100"
FLARESOLVERR_ENDPOINT Endpoint URL to your FlareSolverr instance (eg. "http://192.168.1.55:8191/v1"). If you use this, MAX_RESPONSE_SIZE_MB won't be honored since it can't stream content. For most effectiveness, use a residential IP endpoint
KEEP_ALL_URL_CONTENT Whether to feed the AI all URL content from the whole conversation thread. The website result is cached in memory. If you only want it to know about the current message's URL content (due to context size or cost), set to "FALSE". Default: "TRUE"
MATTERMOST_IGNORE_SENDER_ID The user ID of a user to ignore (optional, useful if you have multiple chatbots that are not real bot accounts to prevent endless loops). Supports multiple, separated by comma
MATTERMOST_PORT The port of your Mattermost server. Default: "443"
MATTERMOST_SCHEME The scheme of the connection. Default: "https"
MATTERMOST_BASEPATH The basepath of your Mattermost server. Default: "/api/v4"
MATTERMOST_CERT_VERIFY Cert verification. Default: True (also: string path to your certificate file)
AI_API_BASEURL AI API Base URL. Default: None (which will use "https://api.openai.com/v1/"). Useful if you want to use a different AI with OpenAI compatible endpoint
LOG_LEVEL The log level. Default: "INFO"
LOG_LEVEL_ROOT The root log level (for other modules than this chatbot). Default: "INFO"

Usage

Run the script:

python3.12 chatbot.py

The chatbot will connect to the Mattermost server and start listening for messages. When a user mentions the chatbot in a message or sends a direct message to the chatbot, the chatbot will process the message, extract content from links (if any), process images, PDFs and other files, handle image content using the Vision API, if necessary queries the DALL-E-3 API, and send the response back to the Mattermost channel.

Note: If you don't trust your users at all, it's recommended to disable the URL/image grabbing feature, even though the chatbot filters out local addresses and IPs.

Running with Docker

You can also run the chatbot using Docker. Use the following command to run the chatbot container:

docker run -d --name chatbotgpt \
  -e AI_API_KEY="your_ai_api_key" \
  -e AI_MODEL="gpt-4-turbo" \
  -e MATTERMOST_URL="mattermostinstance.example.com" \
  -e MATTERMOST_TOKEN="your_mattermost_token" \
  -e MAX_TOKENS="4096" \
  -e TEMPERATURE="1" \
  ghcr.io/elehiggle/chatgptmattermostchatbot:latest

Using DALL-E-3 image generation

Mattermost DALL-E-3 chat with bot example

The AI will generate pictures for you. If you start your message with a "#" it will try to use your prompt as is without any modification by the API.

How does this bot differ from the official Mattermost AI plugin?

The official Mattermost AI plugin is a great way to integrate AI into your Mattermost server with its own advantages, like a nice UI. However, the features differ greatly. It requires direct access to the server, whereas this bot merely needs a bot token and can be conveniently used in a Docker environment.

Also there is:

  • no fine granular control over certain settings (especially AI settings)
  • no CAPTCHA bypass
  • no image generation
  • no Vision API
  • no PDF parsing
  • no YouTube transcript fetching
  • and more

While the official plugin certainly will improve over time, this bot here will too and there will be certain features that will absolutely never make it into the official plugin, due to it primarily focusing on features for developers like function calling to retrieve GitHub issues, for example.

Known Issues

  • Typing indicator is only sent to the channel, not the conversation thread. There is some issue I haven't figured out yet. I even prefer it this way, but mobile users can't see the channel while in a thread.

Other than that, while the chatbot works great for me, there might still be some bugs lurking inside. I have done my best to address them, but if you encounter any issues, please let me know!

Monkey Patch

Please note that the monkey patch in the code is necessary due to some SSL errors that occur because of a mistake within the mattermostdriver library. The patch ensures that the chatbot can establish a secure connection with the Mattermost server.

Related Projects

Anthropic Claude 3 Mattermost Chatbot

Contributing

Contributions are welcome! If you find any issues or have suggestions for improvements, please open an issue or submit a pull request.

License

This project is licensed under the MIT License.

Acknowledgements

About

An AI-powered Mattermost ChatGPT chatbot that utilizes the OpenAI API to provide helpful, contextual responses to user messages, extract text from links, and describe or generate images. With Docker support!

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published