Completed
Push — master ( 8041c5...727159 )
by John
01:12
created

new_slack()   A

Complexity

Conditions 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 1
dl 0
loc 6
rs 9.4285
1
#!ruby
2
3
require 'ringcentral_sdk'
4
require 'glip-poster'
5
require 'pp'
6
7
require 'slack_poster'
8
9
# Set your credentials in the .env file
10
# Use the rc_config_sample.env.txt file as a scaffold
11
12
config = RingCentralSdk::REST::Config.new.load_dotenv
13
14
client = RingCentralSdk::REST::Client.new
15
client.app_config(config.app)
16
client.authorize_user(config.user)
17
18
# An SMS event poster. Takes a message store subscription
19
# event and posts inbound SMS as chats.
20
class RcEventSMSChatPoster
21
  attr_accessor :event
22
  def initialize(client, posters=[], event_data={})
23
    @client = client
24
    @posters = posters
25
    @event = RingCentralSdk::REST::Event.new event_data
26
    @retriever = RingCentralSdk::REST::MessagesRetriever.new @client
27
  end
28
29
  def post_message()
30
    return unless @event.new_sms_count > 0
31
    messages = @retriever.retrieve_for_event(@event, {direction: 'Inbound'})
32
    messages.each do |message|
33
      post_message_to_chat message
34
    end
35
  end
36
37
  def post_message_to_chat(rec)
38
    text = 'SMS from ' + rec['from']['phoneNumber'] \
39
      + "\n* Time: " + rec['creationTime'] + "\n* Message: " + rec['subject']
40
    @posters.each { |v| v.send_message(text) }
41
  end
42
end
43
44
# An observer object that uses RcEventSMSChatPoster to post
45
# to multiple chat posters
46
class RcSmsToChatObserver
47
  def initialize(client, posters)
48
    @client = client
49
    @posters = posters
50
  end
51
52
  def update(message)
53
    event = RcEventSMSChatPoster.new(@client, @posters, message)
54
    event.post_message
55
    puts JSON.dump(message)
56
  end
57
end
58
59
def new_glip(config)
60
  glip = Glip::Poster.new(config.env.data['RC_DEMO_GLIP_WEBHOOK_URL'])
61
  glip.options[:icon] = config.env.data['RC_DEMO_GLIP_WEBHOOK_ICON']
62
  glip.options[:activity] = 'New Inbound SMS'
63
64
  body = "* event_filter: extension/message-store?messageType=SMS\n* actions: post SMS messages to Glip team"
65
66
  glip.send_message(body, {
67
    activity: 'RingCentral subscription initiated',
68
  })
69
  return glip
70
end
71
72
def new_slack()
73
  url = 'https://hooks.slack.com/services/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'
74
  poster = Slack::Poster.new(url)
75
  poster.send_message('*RingCentral subscription initiated!*')
76
  return poster
77
end
78
79
def run_subscription(config, client)
80
  # Create an observable subscription and add your observer
81
  sub = client.create_subscription()
82
  sub.subscribe(['/restapi/v1.0/account/~/extension/~/message-store'])
83
84
  # Create and add first chat poster
85
  posters = []
86
  posters.push new_glip(config)
87
  posters.push new_slack()
88
89
  # Add observer
90
  sub.add_observer(RcSmsToChatObserver.new(client, posters))
91
92
  # Run until key is clicked
93
  puts "Click any key to finish"
94
  stop_script = gets
95
96
  # End the subscription
97
  sub.destroy()
98
end
99
100
run_subscription(config, client)
101
102
puts "DONE"
103