matrix-chatgpt-bot/main.py

37 lines
1.1 KiB
Python

import simplematrixbotlib as botlib
import openai
creds = botlib.Creds("https://matrix.example.com", "bot_user_name", "password")
config = botlib.Config()
config.encryption_enabled = True
config.ignore_unverified_devices = True
config.store_path = './crypto_store/'
bot = botlib.Bot(creds, config)
# Set your prefix here. ! means that every message beginning with ! will tell the bot to respond to it
PREFIX = "!"
@bot.listener.on_message_event
async def request(room, message):
match = botlib.MessageMatch(room, message, bot, PREFIX)
if match.is_not_from_this_bot() and match.prefix():
openai.api_key = "OPENAI TOKEN HERE"
messagetobot = " ".join(arg for arg in match.args())
response = openai.Completion.create(
engine = "text-davinci-002",
prompt = messagetobot,
temperature = 0.7,
max_tokens = 709,
top_p = 1,
frequency_penalty = 0,
presence_penalty = 0
)
responsetomatrix = response.choices[0].text
await bot.api.send_markdown_message(
room.room_id, responsetomatrix
)
bot.run()