问题
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?
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
)
回答1:
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
I removed your account_id, auth_token and phone numbers, so you need to add them back
来源:https://stackoverflow.com/questions/48858646/twilio-sms-using-ruby