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

Laravel路由访问时循环切换SMS内容发送的实现方案咨询

问题:Laravel路由访问时循环使用短信内容发送短信

现有Laravel项目中,/numbers路由对应NumberController的get_numbers_from_ringba方法,访问该路由时会给所有用户发送短信。需求是每次访问路由时,依次使用$contents数组中的元素作为短信内容:第一次用第1个元素,第二次用第2个,直到数组最后一个元素。

解决方案

核心思路是用持久化存储记录当前使用的内容索引——因为HTTP请求是无状态的,需要在请求之间保存这个状态。这里推荐用Laravel的Cache(缓存)实现,轻量且便捷。

修改后的控制器代码

public function get_numbers_from_ringba()
{
    $numbers = Number::all();
    $contents = [
        "We apologize that your call was dropped. We have qualified you for a simple, low-cost health insurance plan. Contact us at +18883470772 again within 1 minute to find out more about your free, no-risk health insurance plan!",
        "Hi, Congratulations! As an apology for the dropped call, We are currently offering you a limited time 20% discount on the ACA Insurance Plan, just for you! Please reach us again at +18883470772. Grab the Lifetime HealthCare Opportunity within a Minute!",
        "Still not Happy with our 20% discount? We have reserved a special enrollment opportunity just for you and your family (all-inclusive medical protection & 25% Discount)! Reduce your chances of becoming ill or injured with ACA, as 14.5 million US citizens have done. Call Now: +18883470772 before the offer expires Today!",
        "We do not want to bother you, just following up about our ACA plan. We can help get your ACA health insurance fixed so your life is more manageable. If you apply today, you might be able to get free health insurance through the Marketplace. Call us +18883470772",
        "Hey there! It’s our final follow-up from our end. Do you still wish to proceed? We are always here to assist you. Call us +18883470772 without any hesitation."
    ];
    
    // 1. 获取当前缓存的索引,默认从0开始(对应数组第一个元素)
    $currentIndex = Cache::get('sms_content_index', 0);
    
    // 2. 处理索引边界:超出数组长度时可选择重置或停止发送
    if ($currentIndex >= count($contents)) {
        $currentIndex = 0; // 重置为0循环使用
        // 若不需要循环,可替换为:return view("Dashboard.Numbers.numbers", ["numbers" => $numbers])->with('msg', '所有短信内容已用完');
    }
    
    // 3. 获取当前要发送的短信内容
    $currentContent = $contents[$currentIndex];
    
    // 4. 更新缓存索引,为下一次访问做准备
    Cache::put('sms_content_index', $currentIndex + 1, now()->addYears(1)); // 长期缓存,除非手动清除
    
    // 5. 执行短信发送逻辑
    $twilio_sid = getenv("TWILIO_SID");
    $twilio_token = getenv("TWILIO_TOKEN");
    $client = new Client($twilio_sid, $twilio_token);
    
    foreach ($numbers as $number) {
        $client->messages->create(
            $number->callerId,
            [
                "from" => "+18882998227",
                "body" => $currentContent
            ]
        );
    }
    
    return view("Dashboard.Numbers.numbers", [
        "numbers" => $numbers
    ]);
}

关键说明

  • 缓存存储索引:用Cache::get和Cache::put保存当前使用的索引,默认从0开始(对应数组第一个元素)。
  • 索引边界处理:当索引超出数组长度时,这里设置为重置为0循环使用;若不需要循环,可改为终止短信发送并提示。
  • 缓存有效期:设置为1年,确保索引不会轻易丢失;如需重置索引,执行Cache::forget('sms_content_index')即可。

替代方案(数据库存储)

如果需要服务器重启后仍保留状态,可以创建一个简单的配置表(如sms_settings),添加current_index字段,将缓存操作替换为数据库的查询和更新逻辑即可。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.06 11:36:59