File size: 808 Bytes
a746d34
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import json
from slack import WebClient, Msg
from transformers import pipeline

# Load environment variables
SLACK_BOT_TOKEN = os.environ['SLACK_BOT_TOKEN']
SLACK_CHANNEL = os.environ['SLACK_CHANNEL']

# Initialize Slack client
client = WebClient(token=SLACK_BOT_TOKEN)

# Initialize ChatGPT model
chat_gpt = pipeline('conversational_ai', model='facebook/bart-base')

def lambda_handler(event, context):
    # Parse Slack event
    event_text = event['event']['text']
    user_id = event['event']['user']

    # Generate response using ChatGPT
    response = chat_gpt(event_text, max_length=100)

    # Post response to Slack channel
    client.chat_postMessage(
        channel=SLACK_CHANNEL,
        text=response
    )

    return {
        'statusCode': 200,
        'statusMessage': 'OK'
    }