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

如何通过WhatsApp发送纯文本格式的数据列表

解决方案

后端PHP实现方案

直接在PHP中预生成目标格式的纯文本,再封装成JSON返回即可:

<?php
// 模拟从业务逻辑/数据库获取的订单数据
$orders = [
    ['customer_code' => '00000563', 'order_code' => '00967'],
    ['customer_code' => '00005531', 'order_code' => '00890'],
    ['customer_code' => '00008239', 'order_code' => '00920'],
    ['customer_code' => '000002315', 'order_code' => '00790']
];

// 构建纯文本内容
$textContent = "Customer code || Order Code\n\n";
foreach ($orders as $order) {
    // 可根据需求调整分隔符前后的空格格式
    $textContent .= "{$order['customer_code']} || {$order['order_code']}\n\n";
}
// 移除最后多余的换行(可选,按WhatsApp显示需求调整)
$textContent = rtrim($textContent, "\n");

// 封装成JSON返回给前端/发送模块
echo json_encode(['message' => $textContent]);
?>

前端JavaScript实现方案

如果需要在前端生成纯文本,用数组拼接的方式高效完成:

// 模拟获取到的订单数据
const orders = [
    { customerCode: '00000563', orderCode: '00967' },
    { customerCode: '00005531', orderCode: '00890' },
    { customerCode: '00008239', orderCode: '00920' },
    { customerCode: '000002315', orderCode: '00790' }
];

// 生成纯文本内容
const lines = ['Customer code || Order Code', '']; // 表头+空行
orders.forEach(order => {
    lines.push(`${order.customerCode} || ${order.orderCode}`, ''); // 每行数据+空行
});
// 移除最后多余的空行(可选)
lines.pop();

const textContent = lines.join('\n');

// 封装成JSON准备发送
const payload = JSON.stringify({ message: textContent });
// 后续执行发送到WhatsApp的逻辑即可

关键注意点

  • 换行统一用\n,WhatsApp能正确识别并换行显示
  • 分隔符||前后的空格可根据视觉需求统一调整格式
  • 数据量较大时建议在后端生成,既保证性能也能统一数据处理逻辑

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.14 16:11:16