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

如何通过AWS Lambda重定向解决Facebook无法推广Viber bot deep link问题

技术实现建议

方案1:3xx重定向方案(需调整现有代码)

  • 不要使用301永久重定向,替换为302临时重定向,避免浏览器缓存重定向规则后无法更新
  • 补充参数获取逻辑,从API Gateway传入的event中提取请求参数,不要直接使用未定义的input变量
  • 新增Facebook爬虫识别逻辑,当检测到请求UA为Facebook爬虫时,返回200状态码的合规HTML页面(带OG标签),避免Facebook审核时识别到非HTTP/HTTPS链接被拒
    示例代码:
exports.handler = async function(event){
    // 识别Facebook爬虫UA
    const userAgent = event.headers['User-Agent'] || ''
    const isFacebookCrawler = userAgent.includes('facebookexternalhit') || userAgent.includes('Facebot')
    
    if (isFacebookCrawler) {
        // 返回带OG标签的合规页面,供Facebook抓取生成卡片
        return {
            statusCode: 200,
            headers: {
                'Content-Type': 'text/html'
            },
            body: `
            <html>
            <head>
            <meta property="og:title" content="联系我们的Viber客服">
            <meta property="og:description" content="点击即可跳转至Viber对话窗口咨询问题">
            <meta property="og:image" content="你的预览图地址">
            </head>
            <body></body>
            </html>
            `
        }
    }
    // 正常用户访问执行重定向逻辑
    const query = event.queryStringParameters || {}
    let result = {}
    switch(true) {
        case Object.keys(query).includes("some parameter"):
           result = {
              statusCode : 302,
              headers : {
                 Location : "viber://pa?chatURI=somebotname",
                 'Cache-Control': 'no-cache, no-store, must-revalidate'
              }
           }
        break;
        // 其余逻辑省略
    }
    return result
}

方案2:前端自动跳转方案(兼容性更强,推荐)

部分Facebook内置浏览器会拦截3xx跳转到自定义协议的请求,可改为Lambda返回HTML页面,通过前端JS触发跳转,同时增加手动跳转兜底按钮,避免跳转失败:

exports.handler = async function(event){
    const userAgent = event.headers['User-Agent'] || ''
    const isFacebookCrawler = userAgent.includes('facebookexternalhit') || userAgent.includes('Facebot')
    
    // 爬虫访问返回OG标签
    if (isFacebookCrawler) {
        return {
            statusCode: 200,
            headers: { 'Content-Type': 'text/html' },
            body: `
            <html>
            <head>
            <meta property="og:title" content="联系我们的Viber客服">
            <meta property="og:description" content="点击即可跳转至Viber对话窗口咨询问题">
            <meta property="og:image" content="你的预览图地址">
            </head>
            <body></body>
            </html>
            `
        }
    }
    // 普通用户返回自动跳转页面
    return {
        statusCode: 200,
        headers: { 'Content-Type': 'text/html' },
        body: `
        <html>
        <head>
        <title>正在跳转至Viber</title>
        <style>
        .container { padding: 2rem; text-align: center; font-size: 1.2rem; }
        .btn { display: inline-block; margin-top: 2rem; padding: 1rem 2rem; background: #7360f2; color: #fff; text-decoration: none; border-radius: 8px; }
        </style>
        </head>
        <body>
        <div class="container">
            <p>正在为您打开Viber,如果没有自动跳转,请点击下方按钮</p>
            <a class="btn" href="viber://pa?chatURI=somebotname">打开Viber</a>
        </div>
        <script>
        window.onload = function() {
            window.location.href = 'viber://pa?chatURI=somebotname'
        }
        </script>
        </body>
        </html>
        `
    }
}

配套配置要求

  • 将Lambda绑定到AWS API Gateway,生成标准HTTPS格式的调用地址,该地址可直接用于Facebook帖子或行动号召按钮,符合Facebook的链接规范
  • 为API Gateway开启CORS配置,避免跨域问题

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.26 01:15:04