-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathclaude.py
62 lines (54 loc) · 2.09 KB
/
claude.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
import asyncio
import json
import os
from slack_sdk.web.async_client import AsyncWebClient
if os.path.exists("claude.json"):
with open("claude.json") as f:
try:
claude_config = json.load(f)
except json.JSONDecodeError:
claude_config = {}
else:
claude_config = {}
class Chatbot:
def __init__(
self,
slack_user_token=claude_config.get("slackUserToken"),
slack_channel_id=claude_config.get("slackChannelId"),
claude_member_id=claude_config.get("claudeMemberId"),
proxy=None,
):
self.client = AsyncWebClient(token=slack_user_token, proxy=proxy)
self.slack_channel_id = slack_channel_id
self.claude_member_id = claude_member_id
async def ask_stream(self, message):
if len(message) < 3000: # Slack truncates message at ~3000 characters
response = await self.client.chat_postMessage(channel=self.slack_channel_id, text=message)
thread_ts = response["ts"]
else:
response = await self.client.chat_postMessage(channel=self.slack_channel_id, text=message[:3000])
thread_ts = response["ts"]
await self.client.chat_postMessage(
channel=self.slack_channel_id,
text=message[3000:],
thread_ts=thread_ts,
)
await self.client.chat_postMessage(
channel=self.slack_channel_id,
text=f'<@{self.claude_member_id}> [assistant](#message)',
thread_ts=thread_ts,
)
while True:
await asyncio.sleep(1)
replies_response = await self.client.conversations_replies(channel=self.slack_channel_id, ts=thread_ts)
all_replies = replies_response["messages"]
for reply in all_replies:
if reply["user"] == self.claude_member_id:
break
else:
continue
if reply["text"].endswith("_Typing…_"):
yield reply["text"][:-11]
else:
yield reply["text"]
break