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