class Dabrab def num_formatter(infobip_msisdn, extra_id) #[{"to"=>{"phoneNumber"=>"375297116638", "emailAddress"=>nil}}] # [{"phone_number": 375298766719}, {"phone_number": 375295730878}] converted_msisdn_list = [] for g in infobip_msisdn begin converted_msisdn_list.append({ "phone_number": g["to"]["phoneNumber"].to_i #"extra_id": extra_id }) rescue p "failed: #{g.to_s}" end end p converted_msisdn_list converted_msisdn_list end def send_responce_formatter(hub_answer) input = {:hub_answer => hub_answer} mess_to_recip = {} messages_sect = [] example = { "bulkId": "2034072219640523072", "messages": [{ "to": "41793026727", "status": { "groupId": 1, "groupName": "PENDING", "id": 26, "name": "MESSAGE_ACCEPTED", "description": "Message sent to next instance" }, "messageId": "2250be2d4219-3af1-78856-aabe-1362af1edfd2" } ] } p "send_responce_formatter(hub_answer)" p hub_answer for a in hub_answer[:body][:res_body]["messages"] status1 = {} if a["processed"] == true && a["accepted"] == true status1 = {"to": {"phoneNumber": a["phone_number"]}, "status": {"groupId": 1, "groupName": "PENDING", "id": 26, "name": "MESSAGE_ACCEPTED", "description": "Message sent to next instance"}, "messageId": a["message_id"]} else status1 = {"to": {"phoneNumber": a["phone_number"]}, "status": {"groupId": 5, "groupName": "REJECTED", "id": 21, "name": "REJECTED_SYSTEM_ERROR", "description": "Unknown error"}, "messageId": a["message_id"]} end messages_sect.append(status1) end p messages_sect mess_to_recip = {"bulkId": "00000000-0000-0000-0000-000000000000", "messages": messages_sect} {:code => 200, :result => "Data processed", :body => {:mess_to_recip => mess_to_recip}} end def delivery_report_transform(hub_report) #{"number"=>"375297116638", "time"=>1588673492000, "status"=>2, "substatus"=>23, "msg_status"=>23033, "message_id"=>"486649ba-a573-4ee9-8f58-018ed20ca6fd", "extra_id"=>"444/0/ServiceModel/ScGetReports.svc/GetReports", "sent_via"=>"viber", "controller"=>"reports", "action"=>"delivery", "report"=>{"number"=>"375297116638", "time"=>1588673492000, "status"=>2, "substatus"=>23, "msg_status"=>23033, "message_id"=>"486649ba-a573-4ee9-8f58-018ed20ca6fd", "extra_id"=>"444/0/ServiceModel/ScGetReports.svc/GetReports", "sent_via"=>"viber"}} p hub_report p "hub_report" data_for_status = { 23011 => {:status_id => 5, :status_grid => 3, :status_grname => "DELIVERED", :status_name => "DELIVERED_TO_HANDSET", :status_descr => "Message delivered by SMS", :error_id => 0, :error_grid => 0, :error_grname => "Ok", :error_name => "NO_ERROR", :error_descr => "No Error", :error_permanent => false }, 23033 => {:status_id => 5, :status_grid => 3, :status_grname => "DELIVERED", :status_name => "DELIVERED_TO_HANDSET", :status_descr => "Message delivered by Viber", :error_id => 0, :error_grid => 0, :error_grname => "Ok", :error_name => "NO_ERROR", :error_descr => "No Error", :error_permanent => false }, 35015 => {:status_id => 15, :status_grid => 4, :status_grname => "EXPIRED", :status_name => "EXPIRED_EXPIRED", :status_descr => "SMS Message TTL EXPIRED", :error_id => 6, :error_grid => 1, :error_grname => "HANDSET_ERRORS", :error_name => "EC_ABSENT_SUBSCRIBER_SM", :error_descr => "Subscriber is absent", :error_permanent => false }, #message SMS expired 36463 => {:status_id => 15, :status_grid => 4, :status_grname => "EXPIRED", :status_name => "EXPIRED_EXPIRED", :status_descr => "Viber Message TTL EXPIRED", :error_id => 6, :error_grid => 1, :error_grname => "HANDSET_ERRORS", :error_name => "EC_ABSENT_SUBSCRIBER_SM", :error_descr => "Subscriber is absent.", :error_permanent => false }, #message Viber expired 36010 => {:status_id => 4, :status_grid => 2, :status_grname => "UNDELIVERABLE", :status_name => "UNDELIVERABLE_REJECTED_OPERATOR", :status_descr => "SMS. Subscriber not exists", :error_id => 6, :error_grid => 1, :error_grname => "HANDSET_ERRORS", :error_name => "EC_UNKNOWN_SUBSCRIBER", :error_descr => "Subscriber not found.", :error_permanent => true }, #SMS subscriber not exists 36131 => {:status_id => 4, :status_grid => 2, :status_grname => "UNDELIVERABLE", :status_name => "UNDELIVERABLE_REJECTED_OPERATOR", :status_descr => "SMS. Unknown subscriber", :error_id => 6, :error_grid => 1, :error_grname => "HANDSET_ERRORS", :error_name => "EC_UNKNOWN_SUBSCRIBER", :error_descr => "Subscriber not found.", :error_permanent => true }, #SMS unknown subscriber 36505 => {:status_id => 4, :status_grid => 2, :status_grname => "UNDELIVERABLE", :status_name => "UNDELIVERABLE_REJECTED_OPERATOR", :status_descr => "Viber. Subscriber not exists", :error_id => 6, :error_grid => 1, :error_grname => "HANDSET_ERRORS", :error_name => "EC_UNKNOWN_SUBSCRIBER", :error_descr => "Subscriber not found.", :error_permanent => true } #Viber subscriber not exists } if data_for_status[hub_report[:msg_status]] != nil && data_for_status[hub_report[:msg_status]] != {} && data_for_status[hub_report[:msg_status]] != [] && data_for_status[hub_report[:msg_status]] != "" report_to_recip = { "results": [ { "bulkId": "00000000-0000-0000-0000-000000000000", "messageId": hub_report[:report][:message_id], "to": hub_report[:report][:number], # "sentAt": "2020-05-05T20:00:00.000+0000", # "doneAt": "2020-05-05T20:00:00.000+0000", "status": { "groupId": data_for_status[hub_report[:msg_status]][:status_grid], "groupName": data_for_status[hub_report[:msg_status]][:status_grname], "id": data_for_status[hub_report[:msg_status]][:status_id], "name": data_for_status[hub_report[:msg_status]][:status_name], "description": data_for_status[hub_report[:msg_status]][:status_descr] }, "error": { "groupId": data_for_status[hub_report[:msg_status]][:error_grid], "groupName": data_for_status[hub_report[:msg_status]][:error_grname], "id": data_for_status[hub_report[:msg_status]][:error_id], "name": data_for_status[hub_report[:msg_status]][:error_name], "description": data_for_status[hub_report[:msg_status]][:error_descr], "permanent": data_for_status[hub_report[:msg_status]][:error_permanent] } } ] } else report_to_recip = { "results": [ { "bulkId": "00000000-0000-0000-0000-000000000000", "messageId": hub_report[:report][:message_id], "to": hub_report[:report][:number], # "sentAt": "2020-05-05T20:00:00.000+0000", # "doneAt": "2020-05-05T20:00:00.000+0000", "status": { "groupId": 2, "groupName": "UNDELIVERABLE_NOT_DELIVERED", "id": 9, "name": "UNDELIVERED", "description": "Unknown error" }, "error": { "groupId": 1, "groupName": "HANDSET_ERRORS", "id": 32, "name": "EC_SM_DELIVERY_FAILURE", "description": "Unknown error", "permanent": false } } ] } end {:code => 200, :result => "Data processed", :body => {:report_to_recip => report_to_recip}} end end