-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvm_fetcher.rb
227 lines (186 loc) · 6.87 KB
/
vm_fetcher.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#!/usr/bin/ruby
# frozen_string_literal: true
require 'httparty'
require 'json'
require 'yaml'
require 'logger'
require 'pp'
require 'fileutils'
@conffile = YAML.load_file('./vm_fetcher.conf')
puts @conffile
dest_dir = @conffile['destination_dir'].to_s
Dir.mkdir(dest_dir) unless Dir.exist?(dest_dir)
log_dir = "logs/"
Dir.mkdir(log_dir) unless Dir.exist?(log_dir)
tmp_dir = @conffile['tempdir'].to_s
Dir.mkdir(tmp_dir) unless Dir.exist?(tmp_dir)
db_dir = 'db/'
Dir.mkdir(db_dir) unless Dir.exist?(db_dir)
db_file = File.join('db', 'messageids')
File.open(db_file, 'a') { |f| f.close } unless File.exist?(db_file)
logfile = "logs/#{@conffile['logfile']}"
logfilerotation = (@conffile['logfilerotation']).to_s
$LOG = Logger.new(logfile, logfilerotation)
$LOG.level = Logger::WARN
####################################################################
### Defining functions
####################################################################
def auth
if @conffile['api_token']
body = { token: @conffile['api_token'], secret: @conffile['api_secret'] }
else
body = { username: @conffile['acct_username'], password: @conffile['acct_password'] }
end
response = HTTParty.post("#{@conffile['portal_url']}/auth/token",
:body => body.to_json,
:timeout => 10,
:headers => { 'Content-Type' => 'application/json' },
:verify => false )
return response
end
def fetch_messages
url = "#{@conffile['portal_url']}/voicemails"
headers = {}
headers[:'Content-Type'] = "application/json"
headers[:'Authorization: Bearer'] = @auth_token
puts headers
response = HTTParty.get(url,
:timeout => 60,
:headers => headers,
:verify => false )
return response
end
def fetch_payload(msgtype, msgid)
if (msgtype == "fax")
url = "#{@conffile['portal_url']}/voicemails/#{msgid}/fax_payload"
elsif (msgtype == "voicemail")
url = "#{@conffile['portal_url']}/voicemails/#{msgid}/voice_payload"
end
headers = {}
headers[:'Content-Type'] = "application/json"
headers[:'Authorization: Bearer'] = @auth_token
puts headers
response = HTTParty.get(url,
:timeout => 10,
:headers => headers,
:verify => false )
return response
end
def file_name(msg)
if msg["message_type"] == "message"
file_ext = "txt"
elsif msg["message_type"] == "fax"
file_ext = msg["fax"].split(".").last
elsif msg["message_type"] == "oncall"
file_ext = msg["voicemail"].split(".").last
elsif msg["message_type"] == "voicemail"
file_ext = msg["voicemail"].split(".").last
end
file_str = "#{@conffile['destination_filename']}.#{file_ext}"
output = file_str.gsub("{id}", msg["id"]).gsub("{caller}", msg["caller"]).gsub("{called}", msg["called"]).gsub("{created}", msg["created_at"]).gsub(" ", "_").gsub("{mailbox}", msg["mailbox"]).gsub("{type}", msg["message_type"]).gsub(':', '.')
return output
end
def transcription_file_name(msg)
file_str = "#{@conffile['destination_filename']}.txt"
output = file_str.gsub("{id}", msg["id"]).gsub("{caller}", msg["caller"]).gsub("{called}", msg["called"]).gsub("{created}", msg["created_at"]).gsub(" ", "_").gsub("{mailbox}", msg["mailbox"]).gsub("{type}", msg["message_type"]).gsub(':', '.')
return output
end
def has_transcription?(msg)
result = false
if msg['transcription'].length > 1
result = true
end
result
end
def message_ids
db_file = File.join('db','messageids')
file = File.open(db_file, 'r')
msgids = file.readlines.map(&:chomp)
file.close
return msgids
end
def message_id_check(msg)
if @conffile['message_redownload']
result = true
else
db_file = File.join('db','messageids')
result = true
if File.exists?(db_file)
$LOG.debug "Checking id #{msg['id']}"
if message_ids.include?(msg['id'])
result = false
else
File.open(db_file, 'a') { |f| f.write("#{msg['id']}\n") }
end
else
File.open(db_file, 'a') { |f| f.write("#{msg['id']}\n") }
end
end
return result
end
def message_type_check(type)
@types || @types = @conffile['message_types']
@types.include?(type)
end
def delete_messge(msgid)
url = "#{@conffile['portal_url']}/voicemails/#{msgid}"
headers = {}
headers[:'Content-Type'] = "application/json"
headers[:'Authorization: Bearer'] = @auth_token
response = HTTParty.delete(url,
:timeout => 10,
:headers => headers,
:verify => false )
return response
end
####################################################################
### Main app logic
####################################################################
puts "Starting up"
auth_resp = auth
puts auth_resp.code
puts auth_resp['access_token']
if auth_resp.code == 200
puts "auth succeeded..setting auth token to #{auth_resp['access_token']}"
@auth_token = auth_resp['access_token']
end
if @auth_token
$LOG.warn "Performing mailbox fetch operation"
puts "received auth token. Good to proceed"
puts "Fetching voicemail mailbox"
mbx = fetch_messages.parsed_response
$LOG.warn "Mailbox retrieved, processing #{mbx.count} messages"
mbx.each do |msg|
begin
puts "Reviewing #{msg["id"]}"
if !message_id_check(msg) || !message_type_check(msg["message_type"])
$LOG.warn "Skipping messsage #{msg["id"]} type: #{msg["message_type"]}"
next
end
$LOG.warn "Downloading #{msg["id"]} message_type: #{msg["message_type"]} created_at:#{msg["created_at"]} from:#{msg['caller']} to:#{msg["called"]} pages:#{msg["pages"]}"
payloadfile = fetch_payload(msg["message_type"], msg["id"]) if msg["message_type"] == "fax" or msg["message_type"] == "voicemail"
puts "Reviewing #{msg}"
filename = file_name(msg)
puts "saving to #{@conffile['destination_dir']}/#{filename}"
file = File.open(File.join(@conffile['destination_dir'].to_s, filename), 'wb')
file.write payloadfile.body if msg["message_type"] == "fax" or msg["message_type"] == "voicemail" or msg["message_type"] == "oncall"
file.write msg["message"] if msg["message_type"] == "message"
file.close
if @conffile['message_transcription_to_file'] == true && has_transcription?(msg)
text_filename = transcription_file_name(msg)
text_file = File.open(File.join(@conffile['destination_dir'].to_s, text_filename), 'wb')
text_file.write msg["transcription"] if msg["message_type"] == "oncall" or msg["message_type"] == "voicemail"
text_file.close
end
if @conffile['delete_messages_after_fetch'] == true
$LOG.warn "Deleting message #{msg["id"]}"
delete_messge(msg["id"])
end
rescue => e
$LOG.warn "Failed to download message #{msg["id"]} error: #{e} "
end
end
else
$LOG.warn "Authentication failed unable to fetch messages"
puts "no auth token..stopping"
end