-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
153 lines (106 loc) · 4.65 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import discord
from discord.ext import commands
import os
from dotenv import load_dotenv
from tinydb import TinyDB, Query
import requests
import json
from utils.manage_events import manage_events
load_dotenv()
DISCORD_BOT_TOKEN = os.getenv("DISCORD_BOT_TOKEN")
INTEGRATION_ID = os.getenv("INTEGRATION_ID")
COMPOSIO_API_KEY = os.getenv("COMPOSIO_API_KEY")
# Create a database to store user data
if not os.path.exists('./db'):
os.makedirs('./db')
if not os.path.exists('./db/temp_user.json'):
with open('./db/temp_user.json', 'w') as f:
f.close()
if not os.path.exists('./db/user.json'):
with open('./db/user.json', 'w') as f:
f.close()
temp_user_db = TinyDB('./db/temp_user.json') # Temporary database to store user data for the current session
user_db = TinyDB('./db/user.json')
intents = discord.Intents.default()
intents.message_content = True
bot = commands.Bot(command_prefix='!', intents=intents)
@bot.event
async def on_ready():
# keeps track of how many guilds / servers the bot is associated with.
guild_count = 0
for guild in bot.guilds:
print(f"- {guild.id} (name: {guild.name})")
guild_count = guild_count + 1
print("SampleDiscordBot is in " + str(guild_count) + " guilds.")
@bot.event
async def on_message(message):
# if message.content.lower() == "hello":
# await message.channel.send("hey bro, what's up?")
await bot.process_commands(message) # Ensures that other commands are processed
@bot.command(name='create_account')
async def _create_account(ctx):
"""
Create an account and save `user_id` and `connected_account_id` in the database.
"""
url = "https://backend.composio.dev/api/v1/connectedAccounts"
user_id = ctx.author.id
# Check if the user already has an account
Account = Query()
is_account = user_db.search(Account.user_id == user_id)
if not is_account:
payload = {"integrationId": INTEGRATION_ID}
headers = {
"X-API-Key": COMPOSIO_API_KEY,
"Content-Type": "application/json"
}
response = requests.post(url, headers=headers, data=json.dumps(payload))
response_data = json.loads(response.text)
temp_user_db.insert({"user_id": user_id, "connected_account_id": response_data["connectedAccountId"]})
await ctx.send(f"Click [here]({response_data['redirectUrl']}) to connect your account.\nOnce you have connected your account, you can use `!calendar` to manage events.")
else:
await ctx.send("You already have an account.")
@bot.command(name='authenticate')
async def _authenticate(ctx):
"""
Create an new account again (because authentication credentials might be expired) and save `user_id` and `connected_account_id` in the database.
"""
url = "https://backend.composio.dev/api/v1/connectedAccounts"
user_id = ctx.author.id
# Check if the user already has an account
Account = Query()
is_account = user_db.search(Account.user_id == user_id)
if is_account:
payload = {"integrationId": INTEGRATION_ID}
headers = {
"X-API-Key": COMPOSIO_API_KEY,
"Content-Type": "application/json"
}
response = requests.post(url, headers=headers, data=json.dumps(payload))
response_data = json.loads(response.text)
user_db.update({"connected_account_id": response_data["connectedAccountId"]}, Account.user_id == user_id)
await ctx.send(f"Click [here]({response_data['redirectUrl']}) to connect your account.\nOnce you have connected your account, you can use `!calendar` to manage events.")
else:
await ctx.send("You don't have an account yet. Please create one using `!create_account`.")
@bot.command(name='calendar')
async def _calendar(ctx, *, message: str):
"""
Manage events on Google Calendar.
"""
user_id = ctx.author.id
# Check if the user has an account
Account = Query()
is_account = user_db.search(Account.user_id == user_id)
if not is_account:
is_temp_account = temp_user_db.search(Account.user_id == user_id)
if not is_temp_account:
await ctx.send("You don't have an account yet. Please create one using `!create_account`.")
return
else:
# Move the temporary account to the main database
user_db.insert(is_temp_account[0])
temp_user_db.remove(Account.user_id == user_id)
await ctx.send("Processing your request...")
connected_account_id = user_db.search(Account.user_id == user_id)[0]["connected_account_id"]
response = manage_events(connected_account_id, message)
await ctx.send(response)
bot.run(DISCORD_BOT_TOKEN)