使用Twilio WhatsApp API发送可滑动轮播消息异常问题排查
WhatsApp交互式轮播消息发送失败问题排查与修复
问题描述
尝试使用Twilio发送交互式WhatsApp轮播消息时,未收到预期的可滑动轮播内容,反而收到两条独立消息:一条包含图片,另一条包含文本。预期效果为可向右滑动并带交互按钮的商品轮播,实际却呈现分离的消息形态。
原代码示例
@app.route('/send_carousel', methods=['GET', 'POST']) def send_carousel(): """ Sends an interactive carousel message to the specified WhatsApp number. """ try: interactive_message = { "type": "interactive", "interactive": { "type": "list", "header": { "type": "text", "text": "Featured Products" }, "body": { "text": "Choose a product to learn more." }, "footer": { "text": "Thank you for shopping with us!" }, "action": { "button": "View Details", "sections": [ { "title": "Products", "rows": [ { "id": "product1", "title": "Profile Picture", "description": "Check out my profile picture." }, { "id": "product2", "title": "Second Item", "description": "Explore more awesome products." } ] } ] } } } message = client.messages.create( from_=from_whatsapp_number, to=to_whatsapp_number, interactive=interactive_message["interactive"] ) logging.info(f"Sent Message SID: {message.sid}") return jsonify({ "message": "Carousel sent successfully", "message_sid": message.sid }), 200 except Exception as e: logging.exception("Exception during message sending") return jsonify({ "error": "An exception occurred while sending message", "details": str(e) }), 500
问题根源
你当前代码使用的是list类型的交互式消息,这是下拉列表样式,并非WhatsApp的商品轮播(Carousel)类型。要实现可滑动的商品轮播,必须使用product类型的交互式消息,且需关联已在Twilio控制台配置好的WhatsApp Business Catalog。
修复步骤
- 配置商品目录:先在Twilio控制台的WhatsApp业务账号中上传商品目录,获取每个商品的
product_retailer_id(商品在目录中的唯一ID)以及目录的catalog_id。 - 修改Payload结构:将交互式消息的
type改为product,并在action中指定关联的目录ID和商品ID列表。
修正后的代码示例
@app.route('/send_carousel', methods=['GET', 'POST']) def send_carousel(): """ Sends a WhatsApp product carousel message. """ try: # 商品轮播的交互式消息结构 interactive_message = { "type": "interactive", "interactive": { "type": "product", "body": { "text": "Check out our featured products!" }, "footer": { "text": "Tap to view details" }, "action": { "catalog_id": "YOUR_CATALOG_ID", # 替换为你的目录ID "product_retailer_ids": [ "PRODUCT_ID_1", # 替换为目录中的商品ID "PRODUCT_ID_2" ] } } } message = client.messages.create( from_=from_whatsapp_number, to=to_whatsapp_number, interactive=interactive_message["interactive"] ) logging.info(f"Sent Message SID: {message.sid}") return jsonify({ "message": "Product carousel sent successfully", "message_sid": message.sid }), 200 except Exception as e: logging.exception("Exception during message sending") return jsonify({ "error": "An exception occurred while sending message", "details": str(e) }), 500
关键注意事项
- 目录与商品ID验证:
catalog_id和product_retailer_ids必须与Twilio控制台中配置的完全一致,不能使用自定义的未存在ID。 - 账号权限:确保你的Twilio账号已开通WhatsApp Business功能,且商品目录已通过审核。
- 设备兼容性:部分旧版本WhatsApp客户端可能不支持商品轮播功能,建议使用最新版本测试。
内容的提问来源于stack exchange,提问作者Harsh Jadhav
相关产品推荐
相关产品推荐

