-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiscord_bot.py
More file actions
93 lines (74 loc) · 2.68 KB
/
discord_bot.py
File metadata and controls
93 lines (74 loc) · 2.68 KB
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
#!/usr/bin/env python3
"""
Claude Agent + Discord Bot
Asks Claude questions and sends responses to Discord
Usage:
python discord_bot.py "What is 25 * 17?"
python discord_bot.py "What time is it?"
"""
import sys
import asyncio
import os
import httpx
# Discord webhook URL
DISCORD_WEBHOOK = os.environ.get("DISCORD_WEBHOOK", "")
# Anthropic API
ANTHROPIC_API_KEY = os.environ.get("ANTHROPIC_API_KEY", "")
async def ask_claude(question: str) -> str:
"""Ask Claude a question and get a response."""
async with httpx.AsyncClient() as client:
response = await client.post(
"https://api.anthropic.com/v1/messages",
headers={
"Content-Type": "application/json",
"x-api-key": ANTHROPIC_API_KEY,
"anthropic-version": "2023-06-01"
},
json={
"model": "claude-3-haiku-20240307",
"max_tokens": 1024,
"messages": [{"role": "user", "content": question}]
},
timeout=30.0
)
if response.status_code == 200:
data = response.json()
return data["content"][0]["text"]
else:
return f"Error: {response.status_code} - {response.text}"
async def send_to_discord(message: str, title: str = None) -> bool:
"""Send a message to Discord."""
payload = {
"username": "Claude Agent",
"embeds": [{
"title": title or "🤖 Agent Response",
"description": message[:4000], # Discord limit
"color": 5814783 # Blue
}]
}
async with httpx.AsyncClient() as client:
response = await client.post(DISCORD_WEBHOOK, json=payload, timeout=10.0)
return response.status_code in [200, 204]
async def main():
if len(sys.argv) < 2:
print("Usage: python discord_bot.py \"Your question here\"")
print("\nExamples:")
print(" python discord_bot.py \"What is 100 divided by 4?\"")
print(" python discord_bot.py \"Write a haiku about coding\"")
print(" python discord_bot.py \"Explain quantum computing in simple terms\"")
return
question = " ".join(sys.argv[1:])
print(f"Question: {question}")
print("Asking Claude...")
# Get response from Claude
answer = await ask_claude(question)
print(f"Claude: {answer[:200]}..." if len(answer) > 200 else f"Claude: {answer}")
# Send to Discord
print("Sending to Discord...")
success = await send_to_discord(answer, f"Q: {question[:100]}")
if success:
print("SUCCESS - Message sent to Discord!")
else:
print("FAILED to send to Discord")
if __name__ == "__main__":
asyncio.run(main())