class Dabrab def dabrabyt_text_field_parse(stri) button_url = "" image = "" button_text = "" text = stri ############# #0 - Text only #1 - Text + Button #2 - Text + Image + Button #3 - image only ############# scheme = 0 scheme_dic = {0 => "Text only", 1 => "Text+Button", 2 => "Text+Image+Button", 3 => "Image only"} ############# output_answer = {} sk_format = {} begin regexp_image1 = /(image:(\S+)[\s\n]?)/ button_url1 = /(button_url:(\S+)[\s\n]?)/ button_text1 = /(button_text:["']([^"]+)["'][\s\n])/ button_url_dat = stri.scan(button_url1) button_text_dat = stri.scan(button_text1) image_dat = stri.scan(regexp_image1) #p button_url_dat #p button_text_dat #p image_dat begin if button_url_dat != [] && button_url_dat != nil && button_url_dat != "" button_url = button_url_dat[0][1] text = text.gsub(button_url_dat[0][0], "") end rescue nil end begin if button_text_dat != [] && button_text_dat != nil && button_text_dat != "" button_text = button_text_dat[0][1] text = text.gsub(button_text_dat[0][0], "") end rescue nil end begin if image_dat != [] && image_dat != nil && image_dat != "" image = image_dat[0][1] text = text.gsub(image_dat[0][0], "") end rescue nil end text = text.gsub(/\s+$/, '').gsub(/\n$/, '') if button_url != "" && button_text != "" && image != "" #111 scheme = 2 elsif button_url != "" && button_text != "" && image == "" #110 scheme = 1 elsif button_url == "" && button_text == "" && image != "" #001 if text == "" scheme = 3 else scheme = 2 button_url = image button_text = "Открыть" end elsif button_url != "" && button_text == "" && image != "" #101 scheme = 2 button_text = "Открыть" elsif button_url == "" && button_text != "" && image != "" #011 scheme = 2 button_url = image elsif button_url == "" && button_text != "" && image == "" #010 scheme = 0 elsif button_url != "" && button_text == "" && image == "" #100 button_text = "Открыть" scheme = 1 else scheme = 0 end case scheme when 0 sk_format = {"text": text, "ttl": 60} when 1 sk_format = {"text": text, "ttl": 60, "caption": button_text, "action": button_url} when 2 sk_format = {"text": text, "ttl": 60, "caption": button_text, "action": button_url, "img": image} when 3 sk_format = {"img": image, "ttl": 60} else nil end #p sk_format output_answer ={:code => 200, :result => "Success", :body => {:to_sk_format => sk_format, :button_url => button_url, :image => image, :button_text => button_text, :text => text, :scheme => scheme, :scheme_dic => scheme_dic[scheme]}} rescue output_answer ={:code => 507, :result => "dabrabyt_text_field_parse: Unknown error"} end output_answer end 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 nil #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_in) #{"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" report_to_recip = [] 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 hub_report_in.key?("msg_status") hub_report = hub_report_in 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.push({ "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.push({ "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 else hub_report_in["_json"].each {|hub_report2| 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.push({ "bulkId": "00000000-0000-0000-0000-000000000000", "messageId": hub_report2["message_id"], "to": hub_report2["number"], # "sentAt": "2020-05-05T20:00:00.000+0000", # "doneAt": "2020-05-05T20:00:00.000+0000", "status": { "groupId": data_for_status[hub_report2["msg_status"]][:status_grid], "groupName": data_for_status[hub_report2["msg_status"]][:status_grname], "id": data_for_status[hub_report2["msg_status"]][:status_id], "name": data_for_status[hub_report2["msg_status"]][:status_name], "description": data_for_status[hub_report2["msg_status"]][:status_descr] }, "error": { "groupId": data_for_status[hub_report2["msg_status"]][:error_grid], "groupName": data_for_status[hub_report2["msg_status"]][:error_grname], "id": data_for_status[hub_report2["msg_status"]][:error_id], "name": data_for_status[hub_report2["msg_status"]][:error_name], "description": data_for_status[hub_report2["msg_status"]][:error_descr], "permanent": data_for_status[hub_report2["msg_status"]][:error_permanent] } }) else report_to_recip.push({ "bulkId": "00000000-0000-0000-0000-000000000000", "messageId": hub_report2["message_id"], "to": hub_report2["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 } end {:code => 200, :result => "Data processed", :body => {:report_to_recip => {"results": report_to_recip}}} end end