Skip to content

Commit c7020d5

Browse files
committed
Add script for cronjob to call meeting API.
1 parent 19dbe64 commit c7020d5

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

Bot/meeting_scheduler.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#!/usr/bin/env python3
2+
3+
import asyncio
4+
import asyncpg
5+
import aiohttp
6+
7+
# --- Configuration ---
8+
PG_USER = "postgres"
9+
PG_PASS = "postgres"
10+
PG_HOST = "localhost"
11+
PG_PORT = "5432"
12+
DB_NAME = "lingo_ai"
13+
API_URL = "http://localhost:8001/meetings/"
14+
SQL_QUERY = 'SELECT "accessToken", "refreshToken", "botName" FROM bot;'
15+
PG_CONN_STRING = f"postgresql://{PG_USER}:{PG_PASS}@{PG_HOST}:{PG_PORT}/{DB_NAME}"
16+
17+
18+
# Fetch access tokens asynchronously
19+
async def fetch_data():
20+
try:
21+
conn = await asyncpg.connect(PG_CONN_STRING)
22+
rows = await conn.fetch(SQL_QUERY)
23+
await conn.close()
24+
return [
25+
{
26+
"access_token": row["accessToken"],
27+
"refresh_token": row["refreshToken"],
28+
"bot_name": row["botName"],
29+
}
30+
for row in rows
31+
]
32+
except Exception as e:
33+
print(f"[ERROR] Database error: {e}")
34+
return []
35+
36+
37+
# Make one request
38+
async def make_request(session, data):
39+
headers = {
40+
"Authorization": f"Bearer {data["access_token"]}",
41+
"Content-Type": "application/json",
42+
}
43+
body = {"refresh_token": data["refresh_token"], "bot_name": data["bot_name"]}
44+
try:
45+
async with session.get(
46+
API_URL, headers=headers, json=body, timeout=10
47+
) as response:
48+
status = response.status
49+
print(f"[INFO] Token: {data['access_token'][:10]}... -> Status: {status}")
50+
except Exception as e:
51+
print(f"[ERROR] Token {data['access_token'][:10]}... failed: {e}")
52+
53+
54+
# Main async workflow
55+
async def main():
56+
tokens = await fetch_data()
57+
async with aiohttp.ClientSession() as session:
58+
tasks = [make_request(session, token) for token in tokens]
59+
await asyncio.gather(*tasks)
60+
61+
62+
# Entry point
63+
if __name__ == "__main__":
64+
asyncio.run(main())

0 commit comments

Comments
 (0)