Spaces:
Running
Running
Update get_msg.py
Browse files- get_msg.py +80 -32
get_msg.py
CHANGED
|
@@ -239,53 +239,94 @@ def format_json_with_timestamps(value: Any, key: str | None = None) -> Any:
|
|
| 239 |
return value
|
| 240 |
|
| 241 |
|
| 242 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 243 |
if len(payload) < 2:
|
| 244 |
-
log_block("
|
| 245 |
return
|
| 246 |
|
| 247 |
event_name = payload[0]
|
| 248 |
-
|
| 249 |
-
log("
|
| 250 |
|
| 251 |
-
if not isinstance(
|
| 252 |
log_block(
|
| 253 |
-
"
|
| 254 |
-
"
|
| 255 |
json.dumps(
|
| 256 |
-
format_json_with_timestamps(
|
| 257 |
ensure_ascii=False,
|
| 258 |
indent=2,
|
| 259 |
),
|
| 260 |
)
|
| 261 |
return
|
| 262 |
|
| 263 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 264 |
if isinstance(entity, dict):
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
)
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 286 |
|
| 287 |
log_block(
|
| 288 |
-
"
|
| 289 |
"詳細JSON",
|
| 290 |
json.dumps(
|
| 291 |
format_json_with_timestamps(payload),
|
|
@@ -573,7 +614,12 @@ def channel_worker(
|
|
| 573 |
phase_started_at = time.monotonic()
|
| 574 |
|
| 575 |
elif event_name == "create":
|
| 576 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 577 |
|
| 578 |
elif event_name == "expired":
|
| 579 |
log("CHANNEL", "expired を受信しました。接続を終了します。")
|
|
@@ -582,6 +628,8 @@ def channel_worker(
|
|
| 582 |
log("CHANNEL", f"エラーで終了: {exc!r}")
|
| 583 |
finally:
|
| 584 |
ws.close()
|
|
|
|
|
|
|
| 585 |
def account_worker(
|
| 586 |
token_holder: dict[str, str],
|
| 587 |
client: ChannelWorksClient,
|
|
|
|
| 239 |
return value
|
| 240 |
|
| 241 |
|
| 242 |
+
def _print_message_entity(tag: str, entity: dict[str, Any]) -> None:
|
| 243 |
+
"""メッセージ entity の中身を整形してログ出力する共通処理。"""
|
| 244 |
+
for key in (
|
| 245 |
+
"chatKey", "id", "mainKey", "channelId", "chatType", "chatId",
|
| 246 |
+
"hubId", "personType", "personId", "language", "plainText",
|
| 247 |
+
"writingType",
|
| 248 |
+
):
|
| 249 |
+
log(tag, f"{key:<11}: {entity.get(key)}")
|
| 250 |
+
for key in ("createdAt", "updatedAt"):
|
| 251 |
+
if key in entity:
|
| 252 |
+
log(tag, f"{key:<11}: {timestamp_to_text(entity[key])}")
|
| 253 |
+
blocks = entity.get("blocks")
|
| 254 |
+
if blocks:
|
| 255 |
+
log(tag, "blocks:")
|
| 256 |
+
for index, block in enumerate(blocks, 1):
|
| 257 |
+
if isinstance(block, dict):
|
| 258 |
+
log(
|
| 259 |
+
tag,
|
| 260 |
+
f" [{index}] type={block.get('type')} "
|
| 261 |
+
f"value={block.get('value')!r}",
|
| 262 |
+
)
|
| 263 |
+
else:
|
| 264 |
+
log(tag, f" [{index}] {block!r}")
|
| 265 |
+
|
| 266 |
+
|
| 267 |
+
def print_push_message(payload: list[Any]) -> None:
|
| 268 |
+
"""push イベントの payload をパースして表示する。
|
| 269 |
+
|
| 270 |
+
想定フォーマット:
|
| 271 |
+
42/desk/channel,["push",{"event":"push","entity":{...},
|
| 272 |
+
"type":"message","refers":{...}}]
|
| 273 |
+
すなわち payload = ["push", {"event":..., "entity":..., "type":..., "refers":...}]
|
| 274 |
+
"""
|
| 275 |
if len(payload) < 2:
|
| 276 |
+
log_block("PUSH", "payload", json.dumps(payload, ensure_ascii=False, indent=2))
|
| 277 |
return
|
| 278 |
|
| 279 |
event_name = payload[0]
|
| 280 |
+
wrapper = payload[1]
|
| 281 |
+
log("PUSH", f"受信イベント: {event_name}")
|
| 282 |
|
| 283 |
+
if not isinstance(wrapper, dict):
|
| 284 |
log_block(
|
| 285 |
+
"PUSH",
|
| 286 |
+
"wrapper",
|
| 287 |
json.dumps(
|
| 288 |
+
format_json_with_timestamps(wrapper),
|
| 289 |
ensure_ascii=False,
|
| 290 |
indent=2,
|
| 291 |
),
|
| 292 |
)
|
| 293 |
return
|
| 294 |
|
| 295 |
+
if "type" in wrapper:
|
| 296 |
+
log("PUSH", f"type : {wrapper.get('type')}")
|
| 297 |
+
if "event" in wrapper:
|
| 298 |
+
log("PUSH", f"event : {wrapper.get('event')}")
|
| 299 |
+
|
| 300 |
+
entity = wrapper.get("entity", {})
|
| 301 |
if isinstance(entity, dict):
|
| 302 |
+
_print_message_entity("PUSH", entity)
|
| 303 |
+
|
| 304 |
+
refers = wrapper.get("refers")
|
| 305 |
+
if isinstance(refers, dict):
|
| 306 |
+
manager = refers.get("manager")
|
| 307 |
+
if isinstance(manager, dict):
|
| 308 |
+
log(
|
| 309 |
+
"PUSH",
|
| 310 |
+
"refers.manager: "
|
| 311 |
+
f"id={manager.get('id')} name={manager.get('name')!r}",
|
| 312 |
+
)
|
| 313 |
+
channel = refers.get("channel")
|
| 314 |
+
if isinstance(channel, dict):
|
| 315 |
+
log(
|
| 316 |
+
"PUSH",
|
| 317 |
+
"refers.channel: "
|
| 318 |
+
f"id={channel.get('id')} name={channel.get('name')!r}",
|
| 319 |
+
)
|
| 320 |
+
group = refers.get("group")
|
| 321 |
+
if isinstance(group, dict):
|
| 322 |
+
log(
|
| 323 |
+
"PUSH",
|
| 324 |
+
"refers.group : "
|
| 325 |
+
f"id={group.get('id')} title={group.get('title')!r}",
|
| 326 |
+
)
|
| 327 |
|
| 328 |
log_block(
|
| 329 |
+
"PUSH",
|
| 330 |
"詳細JSON",
|
| 331 |
json.dumps(
|
| 332 |
format_json_with_timestamps(payload),
|
|
|
|
| 614 |
phase_started_at = time.monotonic()
|
| 615 |
|
| 616 |
elif event_name == "create":
|
| 617 |
+
# create はパースせず生パケットをそのままログ出力
|
| 618 |
+
log_block("CREATE", "raw packet", packet)
|
| 619 |
+
|
| 620 |
+
elif event_name == "push":
|
| 621 |
+
# push をパースしてメッセージを表示
|
| 622 |
+
print_push_message(payload)
|
| 623 |
|
| 624 |
elif event_name == "expired":
|
| 625 |
log("CHANNEL", "expired を受信しました。接続を終了します。")
|
|
|
|
| 628 |
log("CHANNEL", f"エラーで終了: {exc!r}")
|
| 629 |
finally:
|
| 630 |
ws.close()
|
| 631 |
+
|
| 632 |
+
|
| 633 |
def account_worker(
|
| 634 |
token_holder: dict[str, str],
|
| 635 |
client: ChannelWorksClient,
|