Class: Smess::Twilio

Inherits:
Output show all
Includes:
Logging
Defined in:
lib/smess/outputs/twilio.rb

Direct Known Subclasses

TwilioWhatsapp

Instance Attribute Summary collapse

Attributes inherited from Output

#config, #sms

Instance Method Summary collapse

Methods included from Logging

#logger

Constructor Details

#initialize(config) ⇒ Twilio

Returns a new instance of Twilio.



7
8
9
10
# File 'lib/smess/outputs/twilio.rb', line 7

def initialize(config)
  super
  @results = []
end

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



12
13
14
# File 'lib/smess/outputs/twilio.rb', line 12

def api_key
  @api_key
end

#api_secretObject

Returns the value of attribute api_secret.



12
13
14
# File 'lib/smess/outputs/twilio.rb', line 12

def api_secret
  @api_secret
end

#auth_tokenObject

Returns the value of attribute auth_token.



12
13
14
# File 'lib/smess/outputs/twilio.rb', line 12

def auth_token
  @auth_token
end

#callback_urlObject

Returns the value of attribute callback_url.



12
13
14
# File 'lib/smess/outputs/twilio.rb', line 12

def callback_url
  @callback_url
end

#fromObject

Returns the value of attribute from.



12
13
14
# File 'lib/smess/outputs/twilio.rb', line 12

def from
  @from
end

#messaging_service_sidObject

Returns the value of attribute messaging_service_sid.



12
13
14
# File 'lib/smess/outputs/twilio.rb', line 12

def messaging_service_sid
  @messaging_service_sid
end

#sidObject

Returns the value of attribute sid.



12
13
14
# File 'lib/smess/outputs/twilio.rb', line 12

def sid
  @sid
end

#verify_service_sidObject

Returns the value of attribute verify_service_sid.



12
13
14
# File 'lib/smess/outputs/twilio.rb', line 12

def verify_service_sid
  @verify_service_sid
end

Instance Method Details

#check(code) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/smess/outputs/twilio.rb', line 58

def check(code)
  response = client.verify.v2
    .services(verify_service_sid)
    .verification_checks
    .create(to: to, code: code)
  {
    'sid' => response.sid,
    'service_sid' => response.service_sid,
    'account_sid' => response.,
    'to' => response.to,
    'channel' => response.channel,
    'status' => response.status,
    'valid' => response.valid,
    'amount' => response.amount,
    'payee' => response.payee,
    'date_created' => response.date_created,
    'date_updated' => response.date_updated,
    'sna_attempts_error_codes' => response.sna_attempts_error_codes,
  }
end

#deliverObject



30
31
32
# File 'lib/smess/outputs/twilio.rb', line 30

def deliver
  send_one_sms sms.message
end

#send_feedback(message_sid) ⇒ Object



26
27
28
# File 'lib/smess/outputs/twilio.rb', line 26

def send_feedback(message_sid)
  client.messages(message_sid).feedback.create(outcome: "confirmed")
end

#validate_configObject



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/smess/outputs/twilio.rb', line 14

def validate_config
  @sid = config.fetch(:sid)
  @auth_token = config.fetch(:auth_token, nil)
  @api_key = config.fetch(:api_key, nil)
  @api_secret = config.fetch(:api_secret, nil)
  raise "missing API credentials" unless auth_token.present? || (api_key.present? && api_secret.present?)
  @from = config.fetch(:from, nil)
  @messaging_service_sid = config.fetch(:messaging_service_sid, nil)
  @callback_url = config.fetch(:callback_url)
  @verify_service_sid = config.fetch(:verify_service_sid, nil)
end

#verify(using: 'sms') ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/smess/outputs/twilio.rb', line 34

def verify(using: 'sms')
  response = client.verify.v2
    .services(verify_service_sid)
    .verifications
    .create(to: to, channel: using)
  {
    'sid' => response.sid,
    'service_sid' => response.service_sid,
    'account_sid' => response.,
    'to' => response.to,
    'channel' => response.channel,
    'status' => response.status,
    'valid' => response.valid,
    'lookup' => response.lookup,
    'amount' => response.amount,
    'payee' => response.payee,
    'send_code_attempts' => response.send_code_attempts,
    'date_created' => response.date_created,
    'date_updated' => response.date_updated,
    'sna' => response.sna,
    'url' => response.url
  }  
end