Spaces:
Sleeping
Sleeping
dfa
Browse files- command/googlechat.py +46 -12
- command/googlechatthread.py +28 -0
- mysite/interpreter/google_chat.py +64 -0
- mysite/routers/fastapi.py +4 -2
command/googlechat.py
CHANGED
@@ -1,6 +1,9 @@
|
|
1 |
import os
|
2 |
import json
|
|
|
3 |
from google.oauth2 import service_account
|
|
|
|
|
4 |
|
5 |
# 環境変数からサービスアカウントのJSON内容を取得
|
6 |
service_account_info = os.getenv('GOOGLE_APPLICATION_CREDENTIALS_CONTENT')
|
@@ -12,23 +15,54 @@ if service_account_info is None:
|
|
12 |
service_account_info_dict = json.loads(service_account_info)
|
13 |
|
14 |
# サービスアカウント情報を使用して認証情報を作成
|
15 |
-
credentials = service_account.Credentials.from_service_account_info(
|
16 |
-
|
17 |
-
#
|
18 |
-
|
19 |
-
from googleapiclient.discovery import build
|
20 |
|
|
|
21 |
chat_service = build('chat', 'v1', credentials=credentials)
|
22 |
|
23 |
-
#
|
24 |
-
space_name = 'spaces/
|
25 |
-
|
26 |
-
|
|
|
|
|
27 |
}
|
28 |
|
29 |
-
|
30 |
parent=space_name,
|
31 |
-
body=
|
32 |
).execute()
|
33 |
|
34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import os
|
2 |
import json
|
3 |
+
import requests
|
4 |
from google.oauth2 import service_account
|
5 |
+
from google.auth.transport.requests import Request
|
6 |
+
from googleapiclient.discovery import build
|
7 |
|
8 |
# 環境変数からサービスアカウントのJSON内容を取得
|
9 |
service_account_info = os.getenv('GOOGLE_APPLICATION_CREDENTIALS_CONTENT')
|
|
|
15 |
service_account_info_dict = json.loads(service_account_info)
|
16 |
|
17 |
# サービスアカウント情報を使用して認証情報を作成
|
18 |
+
credentials = service_account.Credentials.from_service_account_info(
|
19 |
+
service_account_info_dict,
|
20 |
+
scopes=['https://www.googleapis.com/auth/chat.bot'] # 必要なスコープを指定
|
21 |
+
)
|
|
|
22 |
|
23 |
+
# Google Chat APIクライアントを作成
|
24 |
chat_service = build('chat', 'v1', credentials=credentials)
|
25 |
|
26 |
+
# メッセージを送信するスペースの名前(適切な形式に修正)
|
27 |
+
space_name = 'spaces/AAAAv_S3Bco' # ここを実際のスペースIDに置き換えてください
|
28 |
+
|
29 |
+
# 初回メッセージを送信
|
30 |
+
initial_message = {
|
31 |
+
'text': 'スレッドへ返信に変更テスト'
|
32 |
}
|
33 |
|
34 |
+
initial_response = chat_service.spaces().messages().create(
|
35 |
parent=space_name,
|
36 |
+
body=initial_message
|
37 |
).execute()
|
38 |
|
39 |
+
# スレッドキーを取得
|
40 |
+
thread_name = initial_response['thread']['name']
|
41 |
+
print('Initial message sent. Thread name:', thread_name)
|
42 |
+
|
43 |
+
return thread_name
|
44 |
+
|
45 |
+
|
46 |
+
# 後続のメッセージをスレッド内に送信するためのURLを構築
|
47 |
+
url = f'https://chat.googleapis.com/v1/{space_name}/messages?messageReplyOption=REPLY_MESSAGE_OR_FAIL'
|
48 |
+
|
49 |
+
# 後続のメッセージをスレッド内に送信
|
50 |
+
follow_up_message = {
|
51 |
+
'text': 'スレッドへ返信.',
|
52 |
+
'thread': {
|
53 |
+
'name': thread_name
|
54 |
+
}
|
55 |
+
}
|
56 |
+
|
57 |
+
# アクセストークンを取得
|
58 |
+
credentials.refresh(Request())
|
59 |
+
access_token = credentials.token
|
60 |
+
|
61 |
+
headers = {
|
62 |
+
'Authorization': f'Bearer {access_token}',
|
63 |
+
'Content-Type': 'application/json'
|
64 |
+
}
|
65 |
+
|
66 |
+
response = requests.post(url, headers=headers, data=json.dumps(follow_up_message))
|
67 |
+
|
68 |
+
print('Follow-up message sent:', response.json())
|
command/googlechatthread.py
ADDED
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import requests
|
2 |
+
import json
|
3 |
+
|
4 |
+
# Webhook URL
|
5 |
+
webhook_url = 'https://chat.googleapis.com/v1/spaces/AAAAA/messages?key=YOUR_WEBHOOK_KEY&token=YOUR_WEBHOOK_TOKEN&messageReplyOption=REPLY_MESSAGE_OR_FAIL' # ここを実際のWebhook URLに置き換えてください
|
6 |
+
|
7 |
+
# スレッドキーを設定(任意の一意な値)
|
8 |
+
thread_key = 'example-thread-key'
|
9 |
+
|
10 |
+
# 初回メッセージを送信
|
11 |
+
initial_message = {
|
12 |
+
'text': 'Hello from the Google Chat Webhook!',
|
13 |
+
'threadKey': thread_key
|
14 |
+
}
|
15 |
+
|
16 |
+
response = requests.post(webhook_url, data=json.dumps(initial_message), headers={'Content-Type': 'application/json'})
|
17 |
+
|
18 |
+
print('Initial message response:', response.json())
|
19 |
+
|
20 |
+
# スレッド内に返信メッセージを送信
|
21 |
+
follow_up_message = {
|
22 |
+
'text': 'This is a follow-up message in the same thread.',
|
23 |
+
'threadKey': thread_key
|
24 |
+
}
|
25 |
+
|
26 |
+
response = requests.post(webhook_url, data=json.dumps(follow_up_message), headers={'Content-Type': 'application/json'})
|
27 |
+
|
28 |
+
print('Follow-up message response:', response.json())
|
mysite/interpreter/google_chat.py
CHANGED
@@ -59,3 +59,67 @@ def send_google_chat_card(webhook_url, title, subtitle, link_text, link_url):
|
|
59 |
print("Message posted successfully.")
|
60 |
else:
|
61 |
print(f"Failed to post message: {response.status_code}, {response.text}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
print("Message posted successfully.")
|
60 |
else:
|
61 |
print(f"Failed to post message: {response.status_code}, {response.text}")
|
62 |
+
|
63 |
+
thread_name = response['thread']['name']
|
64 |
+
return thread_name
|
65 |
+
|
66 |
+
def send_google_chat_card_thread(webhook_url, title, subtitle, link_text, link_url,thread_name):
|
67 |
+
headers = {
|
68 |
+
'Content-Type': 'application/json; charset=UTF-8'
|
69 |
+
}
|
70 |
+
|
71 |
+
subtitle = convert_newlines_to_google_chat_format(subtitle)
|
72 |
+
|
73 |
+
card_message = {
|
74 |
+
"cards": [
|
75 |
+
{
|
76 |
+
"header": {
|
77 |
+
"title": title,
|
78 |
+
},
|
79 |
+
"sections": [
|
80 |
+
{
|
81 |
+
"widgets": [
|
82 |
+
{
|
83 |
+
"textParagraph": {
|
84 |
+
"text": subtitle
|
85 |
+
}
|
86 |
+
},
|
87 |
+
{
|
88 |
+
"textParagraph": {
|
89 |
+
"text": "<b>{}</b>".format(link_text)
|
90 |
+
}
|
91 |
+
},
|
92 |
+
{
|
93 |
+
"buttons": [
|
94 |
+
{
|
95 |
+
"textButton": {
|
96 |
+
"text": "Open Link",
|
97 |
+
"onClick": {
|
98 |
+
"openLink": {
|
99 |
+
"url": link_url
|
100 |
+
}
|
101 |
+
}
|
102 |
+
}
|
103 |
+
}
|
104 |
+
]
|
105 |
+
},
|
106 |
+
]
|
107 |
+
}
|
108 |
+
]
|
109 |
+
}
|
110 |
+
],
|
111 |
+
"thread": {
|
112 |
+
"name":thread_name
|
113 |
+
}
|
114 |
+
}
|
115 |
+
|
116 |
+
#https://chat.googleapis.com/v1/spaces/AAAAv_S3Bco/messages?key=AIzaSyDdI0hCZtE6vySjMm-WEfRq3CPzqKqqsHI&token=D635e0eoj7MdO8HV6Ufs1HUZdNiDdz-Eo3Td3OqAFKI&messageReplyOption=REPLY_MESSAGE_OR_FAIL
|
117 |
+
response = requests.post(webhook_url+"&messageReplyOption=REPLY_MESSAGE_OR_FAIL", headers=headers, data=json.dumps(card_message))
|
118 |
+
|
119 |
+
if response.status_code == 200:
|
120 |
+
print("Message posted successfully.")
|
121 |
+
else:
|
122 |
+
print(f"Failed to post message: {response.status_code}, {response.text}")
|
123 |
+
|
124 |
+
thread_name = response['thread']['name']
|
125 |
+
return thread_name
|
mysite/routers/fastapi.py
CHANGED
@@ -12,7 +12,7 @@ from mysite.libs.utilities import validate_signature, no_process_file
|
|
12 |
#from mysite.database.database import ride,create_ride
|
13 |
from controllers.gra_04_database.rides import test_set_lide
|
14 |
from mysite.interpreter.prompt import prompt_genalate,test_prompt
|
15 |
-
from mysite.interpreter.google_chat import send_google_chat_card
|
16 |
#from mysite.interpreter.interpreter import chat_with_interpreter
|
17 |
from controllers.gra_02_openInterpreter.OpenInterpreter import chat_with_interpreter_no_stream
|
18 |
from mysite.appsheet.appsheet import get_senario
|
@@ -207,7 +207,9 @@ def setup_webhook_routes(app: FastAPI):
|
|
207 |
link_text = "データを確認する"
|
208 |
link_url = "https://kenken999-php.hf.space/diamondprice_list.php"
|
209 |
#test_set_lide(subtitle, text)
|
210 |
-
send_google_chat_card(webhook_url, title, subtitle, link_text, link_url)
|
|
|
|
|
211 |
#test case
|
212 |
#########################################################################
|
213 |
first_line = text.split('\n')[0]
|
|
|
12 |
#from mysite.database.database import ride,create_ride
|
13 |
from controllers.gra_04_database.rides import test_set_lide
|
14 |
from mysite.interpreter.prompt import prompt_genalate,test_prompt
|
15 |
+
from mysite.interpreter.google_chat import send_google_chat_card,send_google_chat_card_thread
|
16 |
#from mysite.interpreter.interpreter import chat_with_interpreter
|
17 |
from controllers.gra_02_openInterpreter.OpenInterpreter import chat_with_interpreter_no_stream
|
18 |
from mysite.appsheet.appsheet import get_senario
|
|
|
207 |
link_text = "データを確認する"
|
208 |
link_url = "https://kenken999-php.hf.space/diamondprice_list.php"
|
209 |
#test_set_lide(subtitle, text)
|
210 |
+
thread_name = send_google_chat_card(webhook_url, title, subtitle, link_text, link_url)
|
211 |
+
thread_name = send_google_chat_card(webhook_url, title, subtitle, link_text, link_url,thread_name)
|
212 |
+
return
|
213 |
#test case
|
214 |
#########################################################################
|
215 |
first_line = text.split('\n')[0]
|