Files
assistant-storefront/WEBHOOK_TROUBLESHOOTING.md
Liang XJ 4bd11c0ecc
Some checks failed
Lock Threads / action (push) Has been cancelled
Publish Chatwoot EE docker images / build (linux/amd64, ubuntu-latest) (push) Has been cancelled
Publish Chatwoot EE docker images / build (linux/arm64, ubuntu-22.04-arm) (push) Has been cancelled
Publish Chatwoot EE docker images / merge (push) Has been cancelled
Publish Chatwoot CE docker images / build (linux/amd64, ubuntu-latest) (push) Has been cancelled
Publish Chatwoot CE docker images / build (linux/arm64, ubuntu-22.04-arm) (push) Has been cancelled
Publish Chatwoot CE docker images / merge (push) Has been cancelled
Run Chatwoot CE spec / lint-backend (push) Has been cancelled
Run Chatwoot CE spec / lint-frontend (push) Has been cancelled
Run Chatwoot CE spec / frontend-tests (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (0, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (1, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (10, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (11, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (12, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (13, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (14, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (15, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (2, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (3, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (4, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (5, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (6, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (7, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (8, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (9, 16) (push) Has been cancelled
Run Linux nightly installer / nightly (push) Has been cancelled
Mark stale issues and pull requests / stale (push) Has been cancelled
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

292 lines
6.1 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# Webhook 不触发问题排查
## 快速检查步骤
### 1. 确认服务器已重启 ⚠️
**重要**: 修改代码后必须重启服务器!
```bash
# 如果使用 overmind
overmind restart web
# 或直接重启 Rails server
# Ctrl+C 然后重新启动
bundle exec rails server
```
### 2. 运行调试脚本
```bash
cd /home/lxj/project/chatwoot-develop
bundle exec rails c
# 然后在 console 中执行:
load 'debug_webhook.rb'
```
### 3. 检查 Webhook 配置
在 Rails console 中执行:
```ruby
account = Account.find(2)
webhooks = account.webhooks
# 检查数量
puts "Webhooks 数量: #{webhooks.count}"
# 查看配置
webhooks.each do |w|
puts "ID: #{w.id}"
puts "URL: #{w.webhook_url}"
puts "启用: #{w.active}"
puts "订阅: #{w.subscriptions.inspect}"
end
```
**预期结果**:
- `active: true` (必须启用)
- `subscriptions` 包含 `'message_created'` (必须订阅)
### 4. 手动触发 Webhook 测试
```bash
bundle exec rails c
# 然后执行:
load 'test_webhook_manual.rb'
```
这会:
- 查找最近的 search_image 消息
- 手动构建 webhook payload
- 直接触发 webhook 发送
- 显示结果
### 5. 查看 Webhook 日志
```ruby
# 最近的日志
logs = Account.find(2).webhook_logs.order(created_at: :desc).limit(10)
logs.each do |log|
puts "#{log.created_at} - #{log.event_type} - #{log.status}"
puts "响应码: #{log.response_code}"
puts "错误: #{log.error_message}" if log.error_message
puts "---"
end
```
### 6. 检查最近的消息
```ruby
# 查找 search_image 消息
messages = Message.where(content_type: 'search_image').order(created_at: :desc).limit(5)
messages.each do |msg|
puts "ID: #{msg.id}"
puts "content: '#{msg.content}'"
puts "content_type: #{msg.content_type}"
puts "创建时间: #{msg.created_at}"
puts "---"
end
```
---
## 常见问题和解决方案
### 问题 1: 没有配置 Webhook
**症状**: `webhooks.count == 0`
**解决**:
```ruby
# 创建 Webhook (使用 webhook.site 测试)
account = Account.find(2)
# 1. 访问 https://webhook.site 获取一个临时 URL
# 2. 创建 Webhook 指向该 URL
account.webhooks.create!(
webhook_url: 'https://webhook.site/your-uuid-here', # 替换为实际 URL
subscriptions: ['conversation_created', 'message_created', 'message_updated'],
active: true
)
```
### 问题 2: Webhook 未启用
**症状**: `webhook.active == false`
**解决**:
```ruby
webhook = Webhook.find(1)
webhook.update!(active: true)
```
### 问题 3: 未订阅 message_created 事件
**症状**: `webhook.subscriptions` 不包含 `'message_created'`
**解决**:
```ruby
webhook = Webhook.find(1)
webhook.update!(subscriptions: ['conversation_created', 'message_created', 'message_updated'])
```
### 问题 4: 代码修改未生效
**症状**: 代码已修改但行为未改变
**解决**: 重启服务器!
```bash
# 方法 1: overmind
overmind restart web
# 方法 2: 直接重启
# Ctrl+C 停止服务器
bundle exec rails server
```
### 问题 5: Webhook URL 无法访问
**症状**: 日志显示连接错误
**解决**:
1. 检查 URL 是否正确
2. 确认服务器可以从外网访问
3. 使用 webhook.site 测试
### 问题 6: content 过滤问题
**症状**: search_image 消息不触发 webhook
**验证代码已正确修改**:
```ruby
# 检查 WebhookListener
File.read('app/listeners/webhook_listener.rb') =~ /search_image/
# 应该返回匹配结果
# 查看具体代码
puts File.read('app/listeners/webhook_listener.rb')[/def message_created.*?end/m]
```
应该看到:
```ruby
def message_created(event)
message = extract_message_and_account(event)[0]
inbox = message.inbox
return unless message.webhook_sendable?
# For search_image type, allow webhook even if content is blank
return if message.content.blank? && message.content_type != 'search_image'
payload = message.webhook_data.merge(event: __method__.to_s)
deliver_webhook_payloads(payload, inbox)
end
```
---
## 完整测试流程
### 步骤 1: 重启服务器
```bash
overmind restart web
# 或
bundle exec rails server
```
### 步骤 2: 确认 Webhook 配置
```ruby
# Rails console
account = Account.find(2)
webhooks = account.webhooks
# 如果没有,创建一个
if webhooks.count == 0
account.webhooks.create!(
webhook_url: 'https://webhook.site/your-uuid',
subscriptions: ['message_created'],
active: true
)
end
```
### 步骤 3: 发送测试消息
使用 curl:
```bash
curl -X POST "http://localhost:3000/api/v1/widget/messages?website_token=9n9D3JFHBorFTZLD7cQ49TMg&cw_conversation=xxx&locale=zh_CN" \
-H "Content-Type: application/json" \
-d '{"message":{"content":"","content_type":"search_image","content_attributes":{"url":"https://test.jpg"}}}'
```
或在 /widget_tests 页面上传图片。
### 步骤 4: 立即检查日志
```ruby
# 查看最新消息
message = Message.last
puts "content_type: #{message.content_type}"
puts "content: '#{message.content}'"
# 查看 webhook 日志
logs = Account.find(2).webhook_logs.order(created_at: :desc).limit(5)
logs.each { |log| puts "#{log.event_type} - #{log.status}" }
```
### 步骤 5: 检查 Webhook 接收
- 如果使用 webhook.site刷新页面查看是否收到请求
- 查看请求内容是否包含 search_image 数据
---
## 调试命令汇总
```bash
# 1. 重启服务器
overmind restart web
# 2. 启动 Rails console
bundle exec rails c
# 3. 在 console 中依次执行:
load 'debug_webhook.rb' # 全面诊断
load 'test_webhook_manual.rb' # 手动触发测试
# 4. 发送测试消息
# (另开一个终端)
curl -X POST "http://localhost:3000/api/v1/widget/messages?..." \
-H "Content-Type: application/json" \
-d '{"message":{"content":"","content_type":"search_image","content_attributes":{"url":"https://test.jpg"}}}'
# 5. 检查结果 (在 Rails console 中)
Message.last
Account.find(2).webhook_logs.order(created_at: :desc).first
```
---
## 预期结果
成功的情况下,你应该看到:
1. ✅ 消息创建成功
2. ✅ Dashboard 显示消息
3. ✅ Webhook 日志有新记录 (`status: 'success'``'failed'`)
4. ✅ Webhook 接收端收到请求
如果仍然不工作,请运行 `debug_webhook.rb` 并把输出发给我!