Getting the initial Bot Code Pushed
This commit is contained in:
parent
81a2e16cf9
commit
1d8572e12b
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
scripts/.env
|
37
README.md
37
README.md
@ -1,3 +1,40 @@
|
|||||||
# OpenWebUI-Discordbot
|
# OpenWebUI-Discordbot
|
||||||
|
|
||||||
A Discord bot to communicate with an OpenWebUI instance.
|
A Discord bot to communicate with an OpenWebUI instance.
|
||||||
|
|
||||||
|
A helpful guide to creating your Discord Bot's Token: https://www.writebots.com/discord-bot-token/
|
||||||
|
|
||||||
|
Before running you will need to install the required modules
|
||||||
|
|
||||||
|
```
|
||||||
|
pip install discord.py python-dotenv openai
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
This script:
|
||||||
|
|
||||||
|
Uses environment variables for secure configuration
|
||||||
|
Initializes a Discord bot with message content intents
|
||||||
|
Configures the OpenAI client to point to your OpenWebUI instance
|
||||||
|
Watches for messages that mention the bot
|
||||||
|
When mentioned, it:
|
||||||
|
Removes the mention from the message to get the actual prompt
|
||||||
|
Shows a typing indicator while processing
|
||||||
|
Sends the prompt to OpenWebUI
|
||||||
|
Handles long responses by splitting them if necessary
|
||||||
|
Posts the response back to the Discord channel
|
||||||
|
To use this bot:
|
||||||
|
|
||||||
|
Create a Discord application and bot at the Discord Developer Portal
|
||||||
|
Get your bot token and add it to the .env file
|
||||||
|
Invite the bot to your server with appropriate permissions
|
||||||
|
Set up your OpenWebUI instance and get its API endpoint
|
||||||
|
Run the script
|
||||||
|
You can mention the bot in any channel it has access to with a message like:
|
||||||
|
|
||||||
|
@YourBot What is the weather like today?
|
||||||
|
The bot will process the message and respond with the AI-generated response from your OpenWebUI instance.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
4
scripts/.env.sample
Normal file
4
scripts/.env.sample
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
DISCORD_TOKEN=your_discord_bot_token
|
||||||
|
OPENAI_API_KEY=your_openwebui_api_key
|
||||||
|
OPENWEBUI_API_BASE=http://your_openwebui_instance:port/v1
|
||||||
|
MODEL_NAME="Your_Model_Name"
|
84
scripts/discordbot.py
Normal file
84
scripts/discordbot.py
Normal file
@ -0,0 +1,84 @@
|
|||||||
|
import discord
|
||||||
|
from discord.ext import commands
|
||||||
|
from openai import OpenAI
|
||||||
|
import os
|
||||||
|
from dotenv import load_dotenv
|
||||||
|
|
||||||
|
# Load environment variables
|
||||||
|
load_dotenv()
|
||||||
|
|
||||||
|
# Configure OpenAI client to point to OpenWebUI
|
||||||
|
client = OpenAI(
|
||||||
|
api_key=os.getenv('OPENAI_API_KEY'),
|
||||||
|
base_url=os.getenv('OPENWEBUI_API_BASE') # e.g., "http://localhost:8080/v1"
|
||||||
|
)
|
||||||
|
|
||||||
|
# Initialize Discord bot
|
||||||
|
intents = discord.Intents.default()
|
||||||
|
intents.message_content = True
|
||||||
|
bot = commands.Bot(command_prefix="!", intents=intents)
|
||||||
|
|
||||||
|
async def get_ai_response(prompt):
|
||||||
|
try:
|
||||||
|
response = client.chat.completions.create(
|
||||||
|
model=os.getenv('MODEL_NAME') or "us.anthropic.claude-3-5-sonnet-20241022-v2:0", # OpenWebUI should handle this regardless
|
||||||
|
messages=[
|
||||||
|
{"role": "user", "content": prompt}
|
||||||
|
],
|
||||||
|
temperature=0.7,
|
||||||
|
max_tokens=500
|
||||||
|
)
|
||||||
|
return response.choices[0].message.content
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Error getting AI response: {e}")
|
||||||
|
return "Sorry, I encountered an error while processing your request."
|
||||||
|
|
||||||
|
|
||||||
|
@bot.event
|
||||||
|
async def on_ready():
|
||||||
|
print(f'{bot.user} has connected to Discord!')
|
||||||
|
|
||||||
|
@bot.event
|
||||||
|
async def on_message(message):
|
||||||
|
# Ignore messages from the bot itself
|
||||||
|
if message.author == bot.user:
|
||||||
|
return
|
||||||
|
|
||||||
|
# Check if the bot is mentioned in the message
|
||||||
|
if bot.user in message.mentions:
|
||||||
|
# Remove the bot mention and get the actual prompt
|
||||||
|
prompt = message.content.replace(f'<@{bot.user.id}>', '').strip()
|
||||||
|
|
||||||
|
# If there's no prompt after mentioning the bot
|
||||||
|
if not prompt:
|
||||||
|
await message.channel.send("Hello! How can I help you?")
|
||||||
|
return
|
||||||
|
|
||||||
|
# Show typing indicator while processing
|
||||||
|
async with message.channel.typing():
|
||||||
|
# Get response from OpenWebUI
|
||||||
|
response = await get_ai_response(prompt)
|
||||||
|
|
||||||
|
# Send the response
|
||||||
|
# Split long messages if they exceed Discord's character limit
|
||||||
|
if len(response) > 2000:
|
||||||
|
# Split the response into chunks of 2000 characters
|
||||||
|
chunks = [response[i:i+2000] for i in range(0, len(response), 2000)]
|
||||||
|
for chunk in chunks:
|
||||||
|
await message.channel.send(chunk)
|
||||||
|
else:
|
||||||
|
await message.channel.send(response)
|
||||||
|
|
||||||
|
await bot.process_commands(message)
|
||||||
|
|
||||||
|
def main():
|
||||||
|
# Get the Discord token from environment variables
|
||||||
|
discord_token = os.getenv('DISCORD_TOKEN')
|
||||||
|
if not discord_token:
|
||||||
|
raise ValueError("Discord token not found in environment variables")
|
||||||
|
|
||||||
|
# Run the bot
|
||||||
|
bot.run(discord_token)
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
Loading…
Reference in New Issue
Block a user