├── README.md └── claude.py /README.md: -------------------------------------------------------------------------------- 1 | # Claude_In_Slack_API 2 | 3 | Since Claude API is limited ATM. 4 | 5 | This script simulates API experience with Claude In Slack. 6 | 7 | Claude in slack is currently free, and you can try to use its completion to build fine-tune dataset for davinci. 8 | -------------------------------------------------------------------------------- /claude.py: -------------------------------------------------------------------------------- 1 | import os 2 | import time 3 | from slack_sdk import WebClient 4 | from slack_sdk.errors import SlackApiError 5 | 6 | SLACK_USER_TOKEN = os.environ["SLACK_USER_TOKEN"] 7 | client = WebClient(token=SLACK_USER_TOKEN) 8 | BOT_USER_ID = "" 9 | 10 | def send_message(channel, text): 11 | try: 12 | return client.chat_postMessage(channel=channel, text=text) 13 | except SlackApiError as e: 14 | print(f"Error sending message: {e}") 15 | 16 | def fetch_messages(channel, last_message_timestamp): 17 | response = client.conversations_history(channel=channel, oldest=last_message_timestamp) 18 | return [msg['text'] for msg in response['messages'] if msg['user'] == BOT_USER_ID] 19 | 20 | def get_new_messages(channel, last_message_timestamp): 21 | while True: 22 | messages = fetch_messages(channel, last_message_timestamp) 23 | if messages and not messages[-1].endswith('Typing…_'): 24 | return messages[-1] 25 | time.sleep(5) 26 | 27 | def find_direct_message_channel(user_id): 28 | try: 29 | response = client.conversations_open(users=user_id) 30 | return response['channel']['id'] 31 | except SlackApiError as e: 32 | print(f"Error opening DM channel: {e}") 33 | 34 | def main(): 35 | dm_channel_id = find_direct_message_channel(BOT_USER_ID) 36 | if not dm_channel_id: 37 | print("Could not find DM channel with the bot.") 38 | return 39 | 40 | last_message_timestamp = None 41 | 42 | while True: 43 | prompt = input("\n\n--------------------------------\nUSER: ") 44 | response = send_message(dm_channel_id, prompt) 45 | if response: 46 | last_message_timestamp = response['ts'] 47 | new_message = get_new_messages(dm_channel_id, last_message_timestamp) 48 | print("\n\n---------------------------------\n" + f"Claude: {new_message}") 49 | 50 | if __name__ == "__main__": 51 | main() 52 | --------------------------------------------------------------------------------