File size: 9,849 Bytes
0f43f8a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b2990e4
0f43f8a
 
 
 
 
 
 
 
 
a2ae555
3981bfe
 
 
a2ae555
0f43f8a
 
 
 
 
 
 
 
3238057
0f43f8a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4a363be
 
0307fa7
4a363be
0f43f8a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a5f1115
0f43f8a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10b5954
dd7ce4b
 
 
0f43f8a
 
 
 
 
 
 
 
48b2251
0f43f8a
 
 
 
 
 
 
 
 
 
 
96ce10d
1169d0b
9950e2e
55c5b58
 
 
 
 
 
9950e2e
55c5b58
 
 
0f43f8a
 
 
 
 
 
 
 
 
 
 
 
 
 
6e84fd7
0f43f8a
 
 
a5f1115
b801b97
 
0f43f8a
 
 
a5f1115
 
0f43f8a
 
 
eeb24ed
96ce10d
 
 
0f43f8a
 
 
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204

import os
import sys
import subprocess
import logging
from fastapi import FastAPI, Request, HTTPException,APIRouter
import requests
import json
from datetime import datetime
import importlib
import pkgutil
from mysite.libs.utilities import validate_signature, no_process_file
#from mysite.database.database import ride,create_ride
from routers.gra_04_database.rides import test_set_lide
from mysite.interpreter.prompt import prompt_genalate,test_prompt
from mysite.interpreter.google_chat import send_google_chat_card,send_google_chat_card_thread,send_google_chat_wav
#from mysite.interpreter.interpreter import chat_with_interpreter
from routers.gra_02_openInterpreter.OpenInterpreter import chat_with_interpreter_no_stream
from mysite.appsheet.appsheet import get_senario
import asyncio
from prompts.promps import prompt_for_create_system,prompt,get_prompt
from command.line_get_user_profile import get_user_profile
from command.n8n import post_data,post_data_line
import time
import traceback
from pathlib import Path
from linebot import LineBotApi, WebhookHandler
from linebot.exceptions import InvalidSignatureError
from linebot.models import MessageEvent, TextMessage, TextSendMessage

logger = logging.getLogger(__name__)
##
#router = APIRouter()
router = APIRouter()
#@router.get("/route/webhooks")

@router.post("/webhook")
async def webhook(request: Request):
    import os
    DEBUG=0
    #return
    #logger.info("[Start] ====== LINE webhook ======")
    body = await request.body()
    received_headers = dict(request.headers)
    body_str = body.decode("utf-8")
    logger.info("Received Body: %s", body_str)
    body_json = json.loads(body_str)
    events = body_json.get("events", [])

    webhook_url = os.getenv("chat_url")
    token = os.getenv("token")
    ChannelAccessToken = os.getenv('ChannelAccessToken')
    n8nurl = os.getenv("n8nhook")

    print(webhook_url)
    print(ChannelAccessToken)
    #exit

    thread_name=""
    ###return
    #url = github(token,foldername)
    try:

        for event in events:
            if event["type"] == "message" and event["message"]["type"] == "text":
                user_id = event["source"]["userId"]
                text = event["message"]["text"]
                event_type = event.get('type')
                webhook_event_id = event.get('webhookEventId')
                delivery_context = event.get('deliveryContext', {})
                timestamp = event.get('timestamp')
                mode = event.get('mode')

                # メッセージ情報を取得
                message = event.get('message', {})
                message_type = message.get('type')
                message_id = message.get('id')
                message_text = message.get('text')
                quote_token = message.get('quoteToken')
                chat_id = event.get('source', {}).get('chatId')
                # ソース情報を取得
                source = event.get('source', {})
                source_type = source.get('type')
                user_id = source.get('userId')
                
                # 応答トークンを取得
                reply_token = event.get('replyToken')

                user_name,thmbnail = get_user_profile(user_id,ChannelAccessToken)

                logger.info("Received Headers: %s", user_name)
                logger.info("Received Headers: %s", thmbnail)

                #logger.info("------------------------------------------")
                first_line = text.split('\n')[0]
                #logger.info(f"User ID: {user_id}, Text: {text}")
                #########################################################################
                # 査定用のプロンプト
                #promps,prompt_res = prompt_genalate("返信は日本語で答えて下さい "+text,get_prompt(text))

                #test_set_lide(text,"a1")
                #no_process_file(text, "ai")
                #\r\m
                #########################################################################
                #user_name,thmbnail#
                title = f""" {user_name}様から下記の質問があります"""
                
                subtitle = f"""<b>ユーザーID</b> {user_id}\r\n <b>質問内容</b>\r\n{message_id} {text}"""

                ##
                subtitle = f"""
                <b>ユーザーID:</b>
                    {user_id}
                <b>質問内容:</b> 
                    {text}
                
                """    
                #<b>Webhook Event ID:</b> {webhook_event_id}<br>
                #<b>Delivery Context:</b> {json.dumps(delivery_context)}<br>
                #<b>Timestamp:</b> {timestamp}<br>
                #<b>Mode:</b> {mode}<br>
                #<b>Message Type:</b> {message_type}<br>
                #<b>Message ID:</b> {message_id}<br>
                #<b>Message Text:</b> {message_text}<br>
                #<b>Quote Token:</b> {quote_token}<br>
                #<b>Source Type:</b> {source_type}<br>
                #<b>Reply Token:</b> {reply_token}<br>            
                link_text = "\r\n<b>チャットボット設定用シート</b>\r\n シート用のアプリはチャットから\r\n @リファペディア\r\n と打ち込むと開きます"
                link_url = "https://docs.google.com/spreadsheets/d/13pqP-Ywo5eRlZBsYX2m3ChARG38EoIYOowFd3cWij1c/edit?gid=283940886#gid=283940886"
                #test_set_lide(subtitle, text)
                #thread_name = send_google_chat_card(webhook_url, title, subtitle, link_text, link_url,thmbnail)



                #########################################################################
                ### n8n WorkFlowStart
                #########################################################################
                line_signature = received_headers.get("x-line-signature")
                ####$for debug
                headers = {
                    "Content-Type": "application/json",
                    "X-Line-Signature": line_signature,
                    "Authorization": f"Bearer vzn2zssSEtHb/IVgMbgY1KxLQUfmUXRuiQiQkZLRVsHOeQBp9KsU5/M0i/2XKtw1K+eXN4PyjHQKcG5Vj5l+4e5CGAOQa/veKWdn83UPJQJU17FC9ONucjc84gvNFcRAy4IZcFcMky2PTzazf0KGiFGUYhWQfeY8sLGRXgo3xvw=",
                    "user_id":user_id,
                }
                #/webhook-test/d2d0af6e-5c42-45b6-a923-3bd2d8520e3f
                #d2d0af6e-5c42-45b6-a923-3bd2d8520e3d
                
                
                
                #thread_name = send_google_chat_card_thread(webhook_url, title, subtitle, link_text, link_url,thread_name)
                #return
                #test case
                #########################################################################
                print("mesage is ------------------------------"+message_text)
                #if any(keyword in message for keyword in ["買取方法", "取扱商品", "本日の金価格"]):
                if "金価格" in message_text or "買取方法" in message_text:
                    print("start reply -----------------------------------------------"+reply_token)
                    first_line = text.split('\n')[0]
                    # test_prompt
                    line_bot_api = LineBotApi(ChannelAccessToken)
                    line_bot_api.reply_message(
                        reply_token,
                        TextSendMessage(text=message_text+"買取方法、取扱商品、または本日の金価格に関連するメッセージです 固定メッセージです クレジットは消費しません")
                    )
                    print("End replay -----------------------------------------------")
                    exit
        if not line_signature:
            raise HTTPException(status_code=400, detail="X-Line-Signature header is missing.")

        if not validate_signature(body.decode("utf-8"), line_signature, os.getenv("ChannelSecret")):
            raise HTTPException(status_code=400, detail="Invalid signature.")

        if not os.getenv("WEBHOOK_URL") or not os.getenv("WEBHOOK_URL").startswith("https://"):
            raise HTTPException(status_code=400, detail="Invalid webhook URL")

        headers = {
            "Content-Type": "application/json",
            "X-Line-Signature": line_signature,
            "Authorization": f"Bearer {os.getenv('ChannelAccessToken')}",
        }
        #
        logger.info("Forwarding to URL: %s", os.getenv("WEBHOOK_URL"))
        logger.info("Forwarding Headers: %s", headers)
        logger.info("Forwarding Body: %s", body.decode("utf-8"))
        print("-------------------------------------------------------------------")
        response = requests.post("https://api-mebo.dev/line/events/397cca5b-1a1c-4cee-b207-3b2b4fe642ab191ab9d06f32b7/3491d43d-f3e3-4c90-be9e-70fd61783a1b1919394918b1ac", headers=headers, data=body)
        #responses = requests.post(os.getenv("WEBHOOK_GAS"), headers=headers, data=body)
        logger.info("Response Code: %s", response.status_code)
        logger.info("Response Content: %s", response.text)
        logger.info("Response Headers: %s", response.headers)
        print("-------------------------------------------------------------------")
        
        return {"status": "success", "response_content": response.text}#, response.status_code

    except Exception as e:
        print(e)
        error_file = os.path.basename(__file__)  # ファイル名を取得
        error_line = sys._getframe(1).f_lineno  # 行番号を取得
        print(f"Error occurred at file {error_file} on line {error_line}: {str(e)}")
        #raise するとシステムとまるのでアンコメント
        #raise HTTPException(status_code=500, detail=str(e))
        return {"status": "success", "response_content": str(e)}#, response.status_code