File size: 11,051 Bytes
e584a93 f9beea3 86ba7ff e584a93 f9beea3 c8561d4 08258dc b414ebf 08258dc e584a93 08258dc 8c8bded 08258dc 86ba7ff f9beea3 30342e3 f9beea3 2d6b64e 3aab6ab 2d6b64e f9beea3 2d6b64e f9beea3 2d6b64e 0cf5a9a 2d6b64e f9beea3 2d6b64e 3aab6ab f543d5a 4ad3240 f543d5a 4ad3240 f543d5a 86ba7ff 4b8ed59 9b41c77 e584a93 4b8ed59 86ba7ff 4b8ed59 86ba7ff e584a93 86ba7ff c8561d4 4b8ed59 c8561d4 4b8ed59 c8561d4 4b8ed59 c8561d4 4b8ed59 c8561d4 e584a93 4f9ab86 f9beea3 e584a93 f9beea3 e584a93 f9beea3 e584a93 f9beea3 2d6b64e 3aab6ab f9beea3 e584a93 2d6b64e f9beea3 e584a93 f9beea3 2d6b64e 16d31df f010f62 9b41c77 2d6b64e e584a93 f9beea3 e584a93 f9beea3 e584a93 f9beea3 e584a93 f9beea3 e584a93 c8561d4 e584a93 c8561d4 e584a93 | 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 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 | """Entry point for ORTOS AI Consultant.
Webhook handler for Bitrix24 Open Lines (outgoing webhooks).
"""
import os, json, logging, html, urllib.parse, re
from http.server import HTTPServer, BaseHTTPRequestHandler
from dotenv import load_dotenv
logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(message)s")
for _lib in ("httpx", "httpcore"):
logging.getLogger(_lib).setLevel(logging.WARNING)
logger = logging.getLogger(__name__)
load_dotenv()
PORT = int(os.getenv('PORT', '7860'))
BITRIX_WEBHOOK_URL = os.getenv('BITRIX_WEBHOOK_URL')
BOT_ID = int(os.getenv('BOT_ID', '0'))
from bot import process_message
_auto_bot_id = 0
_webhook_auth = {}
def _parse_form(body: bytes) -> dict:
parsed = urllib.parse.parse_qs(body.decode("utf-8", errors="replace"))
out = {}
for key, vals in parsed.items():
val = vals[0] if len(vals) == 1 else vals
brackets = re.findall(r"\[([^\]]+)\]", key)
if brackets:
section = key.split("[")[0]
d = out.setdefault(section, {})
for b in brackets[:-1]:
d = d.setdefault(b, {})
d[brackets[-1]] = val
else:
out[key] = val
return out
def _ensure_bot_id(data: dict):
global _auto_bot_id
if _auto_bot_id:
return
bots = data.get("data", {}).get("BOT", {})
for bid in bots:
try:
_auto_bot_id = int(bid)
logger.info(f"Auto-detected BOT_ID={_auto_bot_id}")
return
except (ValueError, TypeError):
pass
def _send_reply(dialog_id: str, text: str):
bid = _auto_bot_id or BOT_ID
if not bid:
logger.error(f"Cannot send: bot_id={bid}")
return
import httpx
client_id = _webhook_auth.get("application_token", "") if _webhook_auth else ""
# Try 1: webhook URL + CLIENT_ID (the error said "Client ID not specified")
if BITRIX_WEBHOOK_URL and client_id:
url = f"{BITRIX_WEBHOOK_URL}imbot.message.add.json"
try:
resp = httpx.post(url, params={"CLIENT_ID": client_id}, json={
"BOT_ID": bid,
"DIALOG_ID": dialog_id,
"MESSAGE": text,
}, timeout=30)
logger.info(f"Reply (webhook+CLIENT_ID) {dialog_id}: {resp.status_code} {resp.text[:300]}")
if resp.status_code == 200:
return
except Exception as e:
logger.warning(f"Webhook+CLIENT_ID failed: {e}")
# Try 2: webhook URL without CLIENT_ID (might work for im methods)
if BITRIX_WEBHOOK_URL:
url = f"{BITRIX_WEBHOOK_URL}imbot.message.add.json"
try:
resp = httpx.post(url, json={
"BOT_ID": bid,
"DIALOG_ID": dialog_id,
"MESSAGE": text,
}, timeout=30)
logger.info(f"Reply (webhook no CLIENT_ID) {dialog_id}: {resp.status_code} {resp.text[:300]}")
if resp.status_code == 200:
return
except Exception as e:
logger.warning(f"Webhook URL attempt failed: {e}")
# Try 3: im.message.add as user 1
if BITRIX_WEBHOOK_URL:
url = f"{BITRIX_WEBHOOK_URL}im.message.add.json"
try:
resp = httpx.post(url, json={
"DIALOG_ID": dialog_id,
"MESSAGE": text,
}, timeout=30)
logger.info(f"Reply (im.message) {dialog_id}: {resp.status_code} {resp.text[:300]}")
except Exception as e:
logger.error(f"All send methods failed: {e}")
def transfer_to_operator(dialog_id: str):
chat_id = None
if dialog_id.startswith("chat"):
try:
chat_id = int(dialog_id.replace("chat", ""))
except ValueError:
pass
if not chat_id:
logger.error(f"Cannot transfer: no chat_id from {dialog_id}")
return
import httpx
client_id = _webhook_auth.get("application_token", "") if _webhook_auth else ""
# Send notification to user
_send_reply(dialog_id, "Оператор сейчас подключится. Пожалуйста, ожидайте.")
# Transfer chat to contact center
if BITRIX_WEBHOOK_URL:
url = f"{BITRIX_WEBHOOK_URL}imopenlines.bot.session.operator"
try:
resp = httpx.post(url, json={"CHAT_ID": chat_id}, timeout=30)
logger.info(f"Transfer to operator: {resp.status_code} {resp.text[:300]}")
except Exception as e:
logger.error(f"Transfer error: {e}")
class WebhookHandler(BaseHTTPRequestHandler):
def do_GET(self):
if self.path in ("/", "/health"):
self.send_response(200)
self.send_header("Content-Type", "text/plain")
self.end_headers()
self.wfile.write(b"OK")
return
if self.path == "/logs":
from log_store import get_log
entries = get_log()
page = """<!DOCTYPE html><html lang="ru"><head>
<meta charset="utf-8"><title>ORTOS Bot Logs</title>
<style>
body{font-family:sans-serif;margin:20px;background:#111;color:#eee}
table{border-collapse:collapse;width:100%;font-size:13px}
th,td{text-align:left;padding:6px 10px;border-bottom:1px solid #333;vertical-align:top}
th{background:#222;color:#0f0;position:sticky;top:0}
tr:hover{background:#1a1a1a}
.mode{font-weight:bold;padding:2px 6px;border-radius:3px;font-size:11px;white-space:nowrap}
.groq{background:#1a3a1a;color:#4f4}
.local{background:#3a1a1a;color:#f88}
.fallback{background:#3a3a1a;color:#ff4}
.greeting{background:#1a1a3a;color:#44f}
.operator{background:#3a1a3a;color:#f4f}
.q{color:#ffa;max-width:250px;word-break:break-word}
.r{color:#afa;max-width:350px;word-break:break-word}
.detail{color:#888;font-size:11px;margin-top:4px;border-top:1px solid #333;padding-top:4px}
.lbl{color:#666}
.val{color:#eee}
.src{color:#8af}
.err{color:#f44}
summary{cursor:pointer;color:#8af;font-size:12px}
</style></head><body>
<h2>ORTOS Consultant — last 50 interactions</h2>
<table><thead><tr><th>Time</th><th>Mode</th><th>Q</th><th>Response</th><th>RAG</th></tr></thead>"""
for e in entries:
css = e["mode"]
md = e["search_method"]
llm = html.escape(e["llm_model"])
ms = e["timing_ms"]
rag_html = f"<span class='src'>{html.escape(md)}</span>"
rag_html += f"<br><span class='lbl'>LLM:</span> <span class='val'>{llm}</span>"
rag_html += f"<br><span class='lbl'>⏱</span> <span class='val'>{ms}ms</span>"
if e.get("search_details"):
rag_html += "<details><summary>search results</summary>"
for d in e["search_details"]:
rag_html += f"<div class='detail'>"
rag_html += f"<b class='src'>{html.escape(d.get('title',''))}</b>"
br = d.get("bm25_rank")
er = d.get("embed_rank")
rs = d.get("rrf_score")
if br is not None:
rag_html += f"<br><span class='lbl'>BM25 rank:</span> <span class='val'>{br}</span>"
if er is not None:
rag_html += f"<br><span class='lbl'>bge-m3 rank:</span> <span class='val'>{er}</span>"
if rs is not None:
rag_html += f"<br><span class='lbl'>RRF score:</span> <span class='val'>{rs}</span>"
rag_html += "</div>"
rag_html += "</details>"
page += f"<tr><td>{e['time']}</td><td><span class='mode {css}'>{e['mode']}</span></td>"
page += f"<td class='q'>{html.escape(e['question'])}</td>"
page += f"<td class='r'>{html.escape(e['response'])}</td>"
page += f"<td>{rag_html}</td></tr>"
page += "</table></body></html>"
self.send_response(200)
self.send_header("Content-Type", "text/html; charset=utf-8")
self.end_headers()
self.wfile.write(page.encode())
return
self.send_response(404)
self.end_headers()
def do_POST(self):
length = int(self.headers.get("Content-Length", 0))
body = self.rfile.read(length)
path_only = urllib.parse.urlparse(self.path).path
logger.info(f"POST {path_only} event={body.decode('utf-8', errors='replace')[:200]}")
if path_only != "/bitrix/openlines_webhook":
self._json_response(200, {"status": "unknown_path"})
return
data = _parse_form(body)
_ensure_bot_id(data)
global _webhook_auth
_webhook_auth = data.get("auth", {})
event = data.get("event", "")
params = data.get("data", {}).get("PARAMS", {})
logger.info(f"Parsed event={event} dialog={params.get('DIALOG_ID','')} msg='{str(params.get('MESSAGE',''))[:60]}'")
if event == "ONIMBOTJOINCHAT":
dialog_id = params.get("DIALOG_ID", "")
if dialog_id:
welcome = "Здравствуйте! Я — консультант салона ORTOS. Задайте мне вопрос о стельках, ценах, доставке или записи."
_send_reply(dialog_id, welcome)
self._json_response(200, {})
elif event == "ONIMBOTMESSAGEADD":
dialog_id = params.get("DIALOG_ID", "")
text = params.get("MESSAGE", params.get("TEXT", ""))
from_user = params.get("FROM_USER_ID", "")
is_system = params.get("SYSTEM", "N") == "Y"
if text and dialog_id and not is_system and str(from_user) != "0":
logger.info(f"Processing message from user {from_user}: {text[:80]}")
reply = process_message(text, dialog_id)
if reply.startswith("Переход на оператора") or "переведу вас на оператора" in reply.lower():
transfer_to_operator(dialog_id)
else:
_send_reply(dialog_id, reply)
elif not is_system and str(from_user) == "0":
logger.info("Skipping bot's own message")
self._json_response(200, {})
else:
self._json_response(200, {})
def _json_response(self, status, data):
body_b = json.dumps(data, ensure_ascii=False).encode()
self.send_response(status)
self.send_header("Content-Type", "application/json; charset=utf-8")
self.send_header("Content-Length", str(len(body_b)))
self.end_headers()
self.wfile.write(body_b)
def log_message(self, *args):
pass
def main():
server = HTTPServer(('0.0.0.0', PORT), WebhookHandler)
logger.info(f"ORTOS Consultant running on port {PORT}")
logger.info(f"Webhook URL: POST /bitrix/openlines_webhook")
server.serve_forever()
if __name__ == '__main__':
main()
|