You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Odoo集成Facebook Messenger的webhook控制器http/json请求冲突问题

Odoo单路由适配Facebook Webhook两类请求方案

Odoo支持单个控制器同时处理http和json两类请求,你遇到的报错是因为原控制器仅绑定了type='http'的配置,无法适配Facebook推送的JSON类型POST请求,无需拆分路由,通过挂载双路由装饰器即可实现兼容:

实现代码示例

from odoo import http
from odoo.http import request

class CustomFacebookController(http.Controller):
    # 适配Facebook GET校验请求(http类型)
    @http.route('/get_message_from_facebook', type='http', methods=['GET'], auth='none', csrf=False)
    # 适配Facebook JSON事件推送请求(json类型)
    @http.route('/get_message_from_facebook', type='json', methods=['POST'], auth='none', csrf=False)
    def get_message_from_facebook(self, **kwarg):
        # 处理校验逻辑
        if request.httprequest.method == 'GET':
            verify_token = request.params.get('hub.verify_token')
            challenge = request.params.get('hub.challenge')
            # 此处补充你的token校验逻辑,校验通过直接返回challenge值即可
            return challenge
        
        # 处理事件推送逻辑
        if request.httprequest.method == 'POST':
            event_data = request.jsonrequest
            # 此处补充你处理订阅事件的业务逻辑
            return {'status': 'success'}

注意事项

  • 必须配置csrf=False参数,否则Odoo会拦截外部发起的未携带CSRF令牌的POST请求
  • GET类请求的参数通过request.params获取,POST类JSON请求的载荷通过request.jsonrequest获取
  • 无需手动切换控制器的type属性,Odoo会自动根据请求的方法、类型匹配对应路由规则

内容的提问来源于stack exchange,提问作者Daim Sayed

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.06 07:57:04