| Total Complexity | 5 |
| Total Lines | 52 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 1 |
| 1 | require 'lita/adapters/ringcentral/callback' |
||
| 7 | class Connector |
||
| 8 | attr_reader :robot, :client |
||
| 9 | |||
| 10 | def initialize(robot, app_key, app_secret, server, username, extension, password, token, sms_number) |
||
| 11 | @robot = robot |
||
| 12 | @client = RingCentralSdk::REST::Client.new do |config| |
||
| 13 | config.server_url = server |
||
| 14 | config.app_key = app_key |
||
| 15 | config.app_secret = app_secret |
||
| 16 | config.logger = Lita.logger |
||
| 17 | end |
||
| 18 | @username = username |
||
| 19 | @extension = extension |
||
| 20 | @password = password |
||
| 21 | @token = token |
||
| 22 | @sms_number = sms_number |
||
| 23 | @logger_prefix = " -- #{self.class.name}: " |
||
| 24 | end |
||
| 25 | |||
| 26 | def connect |
||
| 27 | client_connect |
||
| 28 | end |
||
| 29 | |||
| 30 | def client_connect |
||
| 31 | Lita.logger.info("#{@logger_prefix}Authenticating with RingCentral.") |
||
| 32 | if @token.nil? |
||
| 33 | @client.authorize_password @username, @extension, @password |
||
| 34 | else |
||
| 35 | @client.set_token @token |
||
| 36 | end |
||
| 37 | |||
| 38 | @subscription = @client.create_subscription |
||
| 39 | @subscription.subscribe([ |
||
| 40 | '/restapi/v1.0/account/~/extension/~/message-store/instant?type=SMS' |
||
| 41 | ]) |
||
| 42 | |||
| 43 | observer = Lita::Adapters::RingCentral::Callback.new @robot |
||
| 44 | @subscription.add_observer observer |
||
| 45 | end |
||
| 46 | |||
| 47 | def message(to_number, strings) |
||
| 48 | Lita.logger.info("#{@logger_prefix}Sending message to #{to_number}") |
||
| 49 | Lita.logger.info MultiJson.encode(strings) |
||
| 50 | strings.each do |s| |
||
| 51 | @client.messages.sms.create( |
||
| 52 | from: @sms_number, |
||
| 53 | to: to_number, |
||
| 54 | text: s |
||
| 55 | ) |
||
| 56 | end |
||
| 57 | end |
||
| 58 | end |
||
| 59 | end |
||
| 61 | end |