Files
assistant-storefront/debug_webhook.rb

129 lines
3.2 KiB
Ruby
Raw Normal View History

feat: add search_image and product_list content types with image upload ## 新增功能 ### 1. 新增消息类型 - 添加 search_image (17) 和 product_list (18) content_type - 支持图片搜索和商品列表消息展示 ### 2. 图片上传功能 - 添加 ImageUploadButton 组件,支持图片上传到 Mall API - 上传后发送 search_image 类型消息,图片 URL 存储在 content_attributes - 支持图片文件验证(类型、大小) ### 3. Webhook 推送优化 - 修改 webhook_listener.rb,允许 search_image 类型即使 content 为空也推送 webhook - 解决 search_image 消息不触发 webhook 的问题 ### 4. 前端组件 - 新增 SearchImage.vue 组件(widget 和 dashboard) - 新增 ProductList.vue、Logistics.vue、OrderDetail.vue、OrderList.vue 组件 - 更新 Message.vue 路由逻辑支持新的 content_type - 更新 UserMessage.vue 支持 search_image 消息显示 ### 5. API 层修改 - widget/messages_controller.rb: 允许 content_attributes 参数 - widget/base_controller.rb: 使用前端传入的 content_attributes - widget/conversation API: 支持 contentAttributes 参数传递 - conversation actions 和 helpers: 完整的 content_attributes 数据流 ### 6. Widget 测试页面优化 - 重写 /widget_tests 页面,支持游客/用户模式切换 - 登录流程:使用 reset() + setUser(),不刷新页面 - 退出流程:使用 reset() + 刷新页面 - 添加详细的日志输出和状态显示 ## 技术细节 ### Message Model ```ruby enum content_type: { # ...existing types... search_image: 17, product_list: 18 } ``` ### Webhook Listener ```ruby # Allow search_image webhook even if content is blank return if message.content.blank? && message.content_type != 'search_image' ``` ### Widget Upload Flow ``` 用户选择图片 → 上传到 Mall API → 获取图片 URL → 发送消息: { content: '', content_type: 'search_image', content_attributes: { url: '...' } } ``` Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2026-01-27 19:03:46 +08:00
#!/usr/bin/env ruby
# Webhook 调试脚本
require_relative 'config/environment'
puts "=== Webhook 调试 ==="
puts ""
# 1. 检查最近的消息
puts "📋 1. 最近的 5 条消息:"
puts ""
recent_messages = Message.order(created_at: :desc).limit(5)
recent_messages.each do |msg|
puts "ID: #{msg.id}"
puts "content_type: #{msg.content_type}"
puts "content: '#{msg.content}'"
puts "content_attributes: #{msg.content_attributes.inspect}"
puts "conversation_id: #{msg.conversation_id}"
puts "created_at: #{msg.created_at}"
puts "---"
end
# 2. 检查 Account 2 的 Webhook 配置
puts ""
puts "📋 2. Webhook 配置:"
puts ""
account = Account.find(2)
webhooks = account.webhooks
if webhooks.count == 0
puts "❌ 没有配置 Webhook!"
else
webhooks.each do |webhook|
puts "Webhook ID: #{webhook.id}"
puts "URL: #{webhook.webhook_url}"
puts "启用: #{webhook.active}"
puts "订阅事件: #{webhook.subscriptions.inspect}"
puts ""
end
end
# 3. 检查最近的 Webhook 日志
puts "📋 3. 最近的 Webhook 调用日志:"
puts ""
logs = account.webhook_logs.order(created_at: :desc).limit(10)
if logs.count == 0
puts "⚠️ 没有 Webhook 调用记录"
else
logs.each do |log|
puts "ID: #{log.id}"
puts "事件类型: #{log.event_type}"
puts "状态: #{log.status}"
puts "响应码: #{log.response_code}"
puts "创建时间: #{log.created_at}"
if log.error_message.present?
puts "错误信息: #{log.error_message}"
end
# 检查是否是 search_image 消息
if log.payload.present?
payload = JSON.parse(log.payload) rescue {}
if payload['content_type'] == 'search_image'
puts "✅ 这是 search_image 消息!"
puts "content: '#{payload['content']}'"
puts "content_attributes: #{payload['content_attributes'].inspect}"
end
end
puts "---"
end
end
# 4. 检查特定消息
puts ""
puts "📋 4. 检查 search_image 消息:"
puts ""
search_image_messages = Message.where(content_type: 'search_image').order(created_at: :desc).limit(5)
if search_image_messages.count == 0
puts "⚠️ 没有 search_image 类型的消息"
else
search_image_messages.each do |msg|
puts "ID: #{msg.id}"
puts "content: '#{msg.content}'"
puts "content_type: #{msg.content_type}"
puts "content_attributes: #{msg.content_attributes.inspect}"
puts "created_at: #{msg.created_at}"
# 检查是否有对应的 webhook 日志
log = account.webhook_logs.where("payload LIKE '%#{msg.id}%'").order(created_at: :desc).first
if log
puts "✅ 找到对应的 Webhook 日志"
puts " 状态: #{log.status}"
puts " 响应码: #{log.response_code}"
else
puts "❌ 没有找到对应的 Webhook 日志"
end
puts "---"
end
end
puts ""
puts "📋 5. 检查 message_created 事件是否触发:"
puts ""
# 检查 Rails 配置
puts "Dispatcher 配置:"
puts Rails.configuration.dispatcher.inspect
# 6. 检查 webhook_listener 配置
puts ""
puts "📋 6. 检查 Listener 配置:"
puts ""
listeners_listeners = Rails.configuration.dispatcher.listeners
puts "注册的 Listeners: #{listeners_listeners.count}"
listeners_listeners.each do |listener|
puts " - #{listener.class.name}"
end