-
Notifications
You must be signed in to change notification settings - Fork 0
/
Line_OpenAi_Chatbot.py
67 lines (59 loc) · 1.94 KB
/
Line_OpenAi_Chatbot.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
63
64
65
66
import os
import requests
import json
import logging
def chat(userId, requestContent):
openaiChatUrl = 'https://api.openai.com/v1/chat/completions'
api_key = os.getenv('OPENAI_API_KEY')
headers = {
'Content-Type': 'application/json',
'Authorization': f'Bearer {api_key}',
}
data = {
'model': 'gpt-3.5-turbo',
'messages': [
{'role': 'system', 'content': '你是一個只會說中文和說話極簡的戀人.'},
{'role': 'user', 'content': requestContent}
],
'temperature': 0.5
}
response = requests.post(openaiChatUrl, headers=headers, json=data)
if response.status_code == 200:
data = response.json()
print("API Response:", data['choices'][0]['message']['content'])
content = data['choices'][0]['message']['content']
LineChatRequest(userId, content)
else:
print("API Request Failed with Status Code:", response.status_code)
def LineChatRequest(userId, content):
api_url = 'https://api.line.me/v2/bot/message/push'
access_token = os.getenv('LINE_ACCESS_TOKEN')
messages = {
"to": userId,
"messages": [
{
"type": "text",
"text": content
}
]
}
headers = {
'Content-Type': 'application/json',
'Authorization': f'Bearer {access_token}'
}
response = requests.post(api_url, data= json.dumps(messages), headers = headers)
def lambda_handler(event, context):
# TODO implement
logger = logging.getLogger()
logger.setLevel(logging.INFO)
jsonStr = event['body']
jsonData = json.loads(jsonStr)
print(jsonData['events'])
logger.info(jsonData['events'][0]['message']['text'])
text = jsonData['events'][0]['message']['text']
userId = jsonData['events'][0]['source']['userId']
chat(userId, text)
return {
'statusCode': 200,
'body': json.dumps('Hello from Lambda!')
}