使用Ruby的Twilio SMS [英] Twilio SMS using Ruby

查看:77
本文介绍了使用Ruby的Twilio SMS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在研究一个程序,该程序可以读取CSV excel文件并过滤要通过短信发送的信息.目前,该程序只能通过短信发送经过过滤的信息的一行.应该进行哪些修改才能分别发送一行?

I've been working on a program that would read a CSV excel file and filter the information to be sent via sms. Currently, the program is only able to send one row of filtered information via sms. What should be modified to be able to send row after row seperately?

当前,代码在第1列和第2列中搜索"medium"和"2".它将发送第0列和第4列.因此结果将为"DT3/SIG,修复Windows" 但不会发送第5行-"90430/RSO",维修灯"

Currently the code searches for 'medium' and '2' on columns 1 and 2. It would send out columns 0 and 4. Hence the results would be "DT3/SIG, "Repair Windows" but it wouldnt send out row 5 - "90430/RSO", "Repair Lights"

require 'rubygems'
require 'twilio-ruby'
require "csv"

def load_alarms
  CSV.read 'alarms.csv', {col_sep: ';'}
end

def filter_by_event_type_and_severity(alarms, event_type, severity)
  alarms.select do |alarm|
    alarm[1] == event_type && alarm[2].to_i == severity.to_i
  end
end

target_alarms = filter_by_event_type_and_severity(
    load_alarms, 'medium', 2)
equipments  = target_alarms.map { |alarm| [alarm[0], alarm[3]] }

p equipments

account_sid = 'ACCOUNT_ID'
auth_token = 'AUTH_TOKEN'
client = Twilio::REST::Client.new account_sid, auth_token

client.api.account.messages.create(
    from: 'SENDER_PHONE',
    to: 'TARGET_PHONE',
    body: equipments
  )

正如我在评论中所写,

推荐答案

您要做的就是遍历从filter_by_event_type_and_severity方法获得的equipments,并通过带有twillo api的人:

As i wrote in the comment, all you need to do is iterate over the equipments you got from the filter_by_event_type_and_severity method, and send them one by one with the twillo api:

require 'rubygems'
require 'twilio-ruby'
require "csv"

def load_alarms
  CSV.read 'alarms.csv', {col_sep: ';'}
end

def filter_by_event_type_and_severity(alarms, event_type, severity)
  alarms.select do |alarm|
    alarm[1] == event_type && alarm[2].to_i == severity.to_i
  end
end

target_alarms = filter_by_event_type_and_severity(load_alarms, 'medium', 2)
equipments  = target_alarms.map { |alarm| [alarm[0], alarm[3]] }

account_sid = 'ACCOUNT_ID'
auth_token = 'AUTH_TOKEN'
client = Twilio::REST::Client.new account_sid, auth_token

# Here is the iteration
equipments.each do |equipment|
  client.api.account.messages.create(
    from: 'SENDER_PHONE',
    to: 'TARGET_PHONE',
    body: equipment
  )
end

我删除了您的account_id,auth_token和电话号码,因此您需要将其重新添加

这篇关于使用Ruby的Twilio SMS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆