1
|
|
|
#!ruby |
2
|
|
|
|
3
|
|
|
require 'ringcentral_sdk' |
4
|
|
|
require 'pp' |
5
|
|
|
|
6
|
|
|
# Set your credentials in the .env file |
7
|
|
|
# Use the rc_config_sample.env.txt file as a scaffold |
8
|
|
|
|
9
|
|
|
config = RingCentralSdk::REST::Config.new.load_dotenv |
10
|
|
|
|
11
|
|
|
client = RingCentralSdk::REST::Client.new |
12
|
|
|
client.app_config(config.app) |
13
|
|
|
client.authorize_user(config.user) |
14
|
|
|
|
15
|
|
|
# An SMS event poster. Takes a message store subscription |
16
|
|
|
# event and posts inbound SMS as chats. |
17
|
|
|
|
18
|
|
|
class RcEventFaxDownloader |
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
|
|
|
@retriever.range = 0.5 # minutes |
26
|
|
|
end |
27
|
|
|
|
28
|
|
|
def download_fax() |
29
|
|
|
puts @event.new_fax_count |
30
|
|
|
return unless @event.new_fax_count > 0 |
31
|
|
|
messages = @retriever.retrieve_for_event @event, direction: 'Inbound', messageType: 'Fax' |
32
|
|
|
messages.each do |message| |
33
|
|
|
pp message |
34
|
|
|
message['attachments'].each do |att| |
35
|
|
|
url = att['uri'] |
36
|
|
|
filename = 'fax_' + url.gsub(%r{^.*restapi/v[^/]+/}, '').gsub(%r{/},'_') |
37
|
|
|
ext = att['contentType'] == 'application/pdf' ? '.pdf' : '' |
38
|
|
|
filename += ext |
39
|
|
|
response_file = @client.http.get url |
40
|
|
|
@posters.each do |poster| |
41
|
|
|
poster.write_file(filename, response_file) |
42
|
|
|
end |
43
|
|
|
end |
44
|
|
|
end |
45
|
|
|
end |
46
|
|
|
end |
47
|
|
|
|
48
|
|
|
# An observer object that uses RcEventSMSChatPoster to post |
49
|
|
|
# to multiple chat posters |
50
|
|
|
class RcDownloadNewFaxObserver |
51
|
|
|
def initialize(client, posters) |
52
|
|
|
@client = client |
53
|
|
|
@posters = posters |
54
|
|
|
end |
55
|
|
|
|
56
|
|
|
def update(message) |
57
|
|
|
puts "DEMO_RECEIVED_NEW_MESSAGE" |
58
|
|
|
pp message |
59
|
|
|
event = RcEventFaxDownloader.new @client, @posters, message |
60
|
|
|
event.download_fax |
61
|
|
|
puts JSON.dump(message) |
62
|
|
|
end |
63
|
|
|
end |
64
|
|
|
|
65
|
|
|
class RcFaxPosterFilesystem |
66
|
|
|
def initialize(dir = '.') |
67
|
|
|
@dir = dir |
68
|
|
|
end |
69
|
|
|
|
70
|
|
|
def write_file(filename, response) |
71
|
|
|
filepath = File.join(@dir, filename) |
72
|
|
|
File.open(filepath, 'wb') { |fp| fp.write(response.body) } |
73
|
|
|
end |
74
|
|
|
end |
75
|
|
|
|
76
|
|
|
def run_subscription(config, client) |
77
|
|
|
# Create an observable subscription and add your observer |
78
|
|
|
sub = client.create_subscription |
79
|
|
|
sub.subscribe ['/restapi/v1.0/account/~/extension/~/message-store'] |
80
|
|
|
|
81
|
|
|
# Create and add first chat poster |
82
|
|
|
posters = [] |
83
|
|
|
posters.push RcFaxPosterFilesystem.new '.' |
84
|
|
|
|
85
|
|
|
# Add observer |
86
|
|
|
sub.add_observer RcDownloadNewFaxObserver.new client, posters |
87
|
|
|
|
88
|
|
|
# Run until key is clicked |
89
|
|
|
puts "Click any key to finish" |
90
|
|
|
stop_script = gets |
91
|
|
|
|
92
|
|
|
# End the subscription |
93
|
|
|
sub.destroy() |
94
|
|
|
end |
95
|
|
|
|
96
|
|
|
run_subscription config, client |
97
|
|
|
|
98
|
|
|
puts "DONE" |
99
|
|
|
|