Created
June 19, 2023 19:57
-
-
Save nathanmargaglio/06125df2e70f584a7c65136f1dfa31f5 to your computer and use it in GitHub Desktop.
Discord Bot + OpenAI + TickTick
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import json | |
import datetime | |
from dotenv import load_dotenv | |
load_dotenv() | |
DISCORD_BOT_TOKEN = os.getenv("DISCORD_BOT_TOKEN") | |
TICKTICK_CLIENT_ID = os.getenv("TICKTICK_CLIENT_ID") | |
TICKTICK_CLIENT_SECRET = os.getenv("TICKTICK_CLIENT_SECRET") | |
TICKTICK_REDIRECT_URI = os.getenv("TICKTICK_REDIRECT_URI") | |
TICKTICK_USERNAME = os.getenv("TICKTICK_USERNAME") | |
TICKTICK_PASSWORD = os.getenv("TICKTICK_PASSWORD") | |
TICKTICK_PROJECT_ID = os.getenv("TICKTICK_PROJECT_ID") | |
TICKTICK_TIMEZONE = os.getenv("TICKTICK_TIMEZONE") | |
import discord | |
import openai | |
from ticktick.oauth2 import OAuth2 | |
from ticktick.api import TickTickClient | |
from schemas import * | |
ticktick_auth_client = OAuth2( | |
client_id=TICKTICK_CLIENT_ID, | |
client_secret=TICKTICK_CLIENT_SECRET, | |
redirect_uri=TICKTICK_REDIRECT_URI | |
) | |
ticktick_client = TickTickClient( | |
TICKTICK_USERNAME, | |
TICKTICK_PASSWORD, | |
ticktick_auth_client | |
) | |
class BotClient(discord.Client): | |
async def on_ready(self): | |
print('Logged on as', self.user) | |
async def on_message(self, message): | |
if message.author == self.user: | |
return | |
messages = [ | |
{"role": "user", "content": f"The current datetime is {datetime.datetime.now().isoformat()}"}, | |
{"role": "user", "content": "When a reminder is set, be sure to set 'dueDate' and 'reminders'."}, | |
{"role": "user", "content": message.content} | |
] | |
functions = [ | |
{ | |
"name": "create_task", | |
"description": "Create a task in TickTick", | |
"parameters": create_task_schema, | |
} | |
] | |
await message.channel.send("Ok! One moment...") | |
response = openai.ChatCompletion.create( | |
model="gpt-3.5-turbo-16k-0613", | |
messages=messages, | |
functions=functions | |
) | |
response_message = response["choices"][0]["message"] | |
if response_message.get("function_call"): | |
available_functions = { | |
"create_task": ticktick_client.task.create, | |
} | |
function_name = response_message["function_call"]["name"] | |
fuction_to_call = available_functions[function_name] | |
function_args = json.loads(response_message["function_call"]["arguments"]) | |
await message.channel.send(f"Calling function {function_name}...") | |
function_response = fuction_to_call({ | |
"projectId": TICKTICK_PROJECT_ID, | |
"timeZone": TICKTICK_TIMEZONE, | |
**function_args | |
}) | |
messages.append(response_message) | |
messages.append( | |
{ | |
"role": "function", | |
"name": function_name, | |
"content": json.dumps(function_response), | |
} | |
) | |
second_response = openai.ChatCompletion.create( | |
model="gpt-3.5-turbo-16k-0613", | |
messages=messages, | |
) | |
second_response_message = second_response["choices"][0]["message"]["content"] | |
await message.channel.send(second_response_message) | |
intents = discord.Intents.default() | |
intents.message_content = True | |
client = BotClient(intents=intents) | |
client.run(DISCORD_BOT_TOKEN) | |
client = discord.Client() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment