grokify /
ringcentral-sdk-ruby
| 1 | #!ruby |
||
| 2 | |||
| 3 | require 'dotenv' |
||
| 4 | require 'ringcentral_sdk' |
||
| 5 | require 'pp' |
||
| 6 | |||
| 7 | puts ENV['ENV_PATH'] |
||
| 8 | |||
| 9 | envPath = ENV['ENV_PATH'] || '.env' |
||
| 10 | # Set your credentials in the .env file |
||
| 11 | # Use the rc_config_sample.env.txt file as a scaffold |
||
| 12 | |||
| 13 | #client = RingCentralSdk::REST::Client.new do |config| |
||
| 14 | # config.load_env = true |
||
| 15 | #end |
||
| 16 | |||
| 17 | puts envPath |
||
| 18 | Dotenv.load envPath |
||
| 19 | |||
| 20 | puts ENV['RINGCENTRAL_USERNAME'] |
||
| 21 | puts ENV['RINGCENTRAL_SERVER_URL'] |
||
| 22 | |||
| 23 | def EnvVal(envVar) |
||
| 24 | envVar = envVar + '_2' |
||
| 25 | ENV[envVar] |
||
| 26 | end |
||
| 27 | |||
| 28 | client = RingCentralSdk::REST::Client.new do |config| |
||
| 29 | config.server_url = EnvVal('RINGCENTRAL_SERVER_URL') |
||
| 30 | config.app_key = EnvVal('RINGCENTRAL_CLIENT_ID') |
||
| 31 | config.app_secret = EnvVal('RINGCENTRAL_CLIENT_SECRET') |
||
| 32 | config.username = EnvVal('RINGCENTRAL_USERNAME') |
||
| 33 | config.extension = EnvVal('RINGCENTRAL_EXTENSION') |
||
| 34 | config.password = EnvVal('RINGCENTRAL_PASSWORD') |
||
| 35 | end |
||
| 36 | |||
| 37 | def getForwardingNumbers(client, accountId, extensionId) |
||
| 38 | client.http.get "account/#{accountId}/extension/#{extensionId}/forwarding-number" |
||
| 39 | end |
||
| 40 | |||
| 41 | def delAnsweringRule(client, accountId, extensionId, answeringRuleId) |
||
| 42 | client.http.delete "account/#{accountId}/extension/#{extensionId}/answering-rule/#{answeringRuleId}" |
||
| 43 | end |
||
| 44 | |||
| 45 | def getAnsweringRules(client, accountId, extensionId) |
||
| 46 | client.http.get "account/#{accountId}/extension/#{extensionId}/answering-rule" |
||
| 47 | end |
||
| 48 | |||
| 49 | def getAnsweringRule(client, accountId, extensionId, answeringRuleId) |
||
| 50 | client.http.get "account/#{accountId}/extension/#{extensionId}/answering-rule/#{answeringRuleId}" |
||
| 51 | end |
||
| 52 | |||
| 53 | # Business Hours Rule - On Routing Extension |
||
| 54 | # Routing Extension, Set to Final Default Extension ID |
||
| 55 | def updateAnsweringRuleOnRoutingExt(client, accountId, extensionId, answeringRuleId, forwardingNumberId) |
||
| 56 | body = { |
||
| 57 | 'enabled' => true, |
||
| 58 | 'forwarding' => { |
||
| 59 | 'notifyMySoftPhones' => true, |
||
| 60 | 'softPhonesRingCount' => 5, |
||
| 61 | 'ringingMode' => 'Sequentially', |
||
| 62 | 'rules': [{ |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 63 | 'index': 1, |
||
| 64 | 'ringCount': 3, |
||
| 65 | 'forwardingNumbers' => [{ |
||
| 66 | 'id' => forwardingNumberId |
||
| 67 | }], |
||
|
0 ignored issues
–
show
|
|||
| 68 | }] |
||
| 69 | } |
||
| 70 | } |
||
| 71 | client.http.put do |req| |
||
| 72 | req.url "account/#{accountId}/extension/#{extensionId}/answering-rule/#{answeringRuleId}" |
||
| 73 | req.headers['Content-Type'] = 'application/json' |
||
| 74 | req.body = body |
||
| 75 | end |
||
| 76 | end |
||
| 77 | |||
| 78 | def updateAnsweringRuleOnInboundExt(client, accountId, extensionId, answeringRuleId, selfFwdNumId, routingFwdNumId) |
||
| 79 | body = { |
||
| 80 | 'enabled' => true, |
||
| 81 | 'forwarding' => { |
||
| 82 | 'notifyMySoftPhones' => true, |
||
| 83 | 'notifyAdminSoftPhones' => false, |
||
| 84 | 'softPhonesRingCount' => 5, |
||
| 85 | 'ringingMode' => 'Sequentially', |
||
| 86 | 'rules': [ |
||
|
0 ignored issues
–
show
|
|||
| 87 | { |
||
| 88 | 'index': 1, |
||
|
0 ignored issues
–
show
|
|||
| 89 | 'ringCount': 3, |
||
| 90 | 'forwardingNumbers' => [{'id' => selfFwdNumId}] |
||
| 91 | }, |
||
|
0 ignored issues
–
show
|
|||
| 92 | { |
||
| 93 | 'index': 2, |
||
| 94 | 'ringCount': 3, |
||
| 95 | 'forwardingNumbers' => [{'id' => routingFwdNumId}] |
||
| 96 | }, |
||
|
0 ignored issues
–
show
|
|||
| 97 | ] |
||
| 98 | } |
||
| 99 | } |
||
| 100 | client.http.put do |req| |
||
| 101 | req.url "account/#{accountId}/extension/#{extensionId}/answering-rule/#{answeringRuleId}" |
||
| 102 | req.headers['Content-Type'] = 'application/json' |
||
| 103 | req.body = body |
||
| 104 | end |
||
| 105 | end |
||
| 106 | |||
| 107 | def createForwardingNumber(client, accountId, extensionId, phoneNumber, label) |
||
| 108 | client.http.post do |req| |
||
| 109 | req.url "account/#{accountId}/extension/#{extensionId}/forwarding-number" |
||
| 110 | req.headers['Content-Type'] = 'application/json' |
||
| 111 | req.body = { |
||
| 112 | 'phoneNumber' => phoneNumber, |
||
| 113 | 'label' => label |
||
| 114 | } |
||
| 115 | end |
||
| 116 | end |
||
| 117 | |||
| 118 | def createCustomCallHandlingRule(client, accountId, extensionId, ruleName, callerIds, fwdId) |
||
| 119 | body = { |
||
| 120 | 'name' => ruleName, |
||
| 121 | 'enabled' => true, |
||
| 122 | 'callers' => [], |
||
| 123 | 'forwarding' => { |
||
| 124 | 'notifyMySoftPhones' => true, |
||
| 125 | 'notifyAdminSoftPhones' => false, |
||
| 126 | 'softPhonesRingCount' => 5, |
||
| 127 | 'ringingMode' => 'Sequentially', |
||
| 128 | 'rules': [ |
||
|
0 ignored issues
–
show
|
|||
| 129 | { |
||
| 130 | 'index': 1, |
||
|
0 ignored issues
–
show
|
|||
| 131 | 'ringCount': 3, |
||
| 132 | 'forwardingNumbers' => [{'id' => fwdId}] |
||
| 133 | }, |
||
|
0 ignored issues
–
show
|
|||
| 134 | ] |
||
| 135 | } |
||
| 136 | } |
||
| 137 | callerIds.each do |callerId| |
||
| 138 | body['callers'].push({'callerId' => callerId}) |
||
| 139 | end |
||
| 140 | client.http.post do |req| |
||
| 141 | req.url "account/#{accountId}/extension/#{extensionId}/answering-rule" |
||
| 142 | req.headers['Content-Type'] = 'application/json' |
||
| 143 | req.body = body |
||
| 144 | end |
||
| 145 | end |
||
| 146 | |||
| 147 | def handleResponse(res) |
||
| 148 | puts res.status |
||
| 149 | pp res.body |
||
| 150 | end |
||
| 151 | |||
| 152 | #handleResponse getForwardingNumbers(client,'~','~') |
||
| 153 | handleResponse getAnsweringRules(client,'~','~') |
||
| 154 | #handleResponse getAnsweringRule(client,'~','~', 'business-hours-rule') |
||
| 155 | #handleResponse getAnsweringRule(client,'~','~', '12345678') |
||
| 156 | #handleResponse delAnsweringRule(client,'~','~', '12345678') |
||
| 157 | |||
| 158 | #handleResponse createCustomCallHandlingRule(client,'~','~','Custom +16505550100', ['+16505550100'], 12345678) |
||
| 159 | #delRules = [12345678,123456789] |
||
| 160 | |||
| 161 | if 1==1 |
||
| 162 | res = getAnsweringRules(client,'~','~') |
||
| 163 | res.body['records'].each do |rec| |
||
| 164 | if rec['type'] == 'Custom' |
||
| 165 | puts rec['id'] |
||
| 166 | pp rec |
||
| 167 | handleResponse delAnsweringRule(client,'~','~', rec['id']) |
||
| 168 | end |
||
| 169 | end |
||
| 170 | end |
||
| 171 | |||
| 172 | if 1==0 |
||
| 173 | handleResponse createForwardingNumber(client, '~', '~', |
||
| 174 | ENV['IVRDEMO_DEFAULT_FINAL_EXTENSION_DIRECT_NUMBER'], |
||
| 175 | ENV['IVRDEMO_DEFAULT_FINAL_EXTENSION_LABEL']) |
||
| 176 | |||
| 177 | handleResponse createForwardingNumber(client, '~', '~', |
||
| 178 | ENV['IVRDEMO_SPECIAL_FINAL_EXTENSION_DIRECT_NUMBER'], |
||
| 179 | ENV['IVRDEMO_SPECIAL_FINAL_EXTENSION_LABEL']) |
||
| 180 | end |
||
| 181 | |||
| 182 | if 1==0 |
||
| 183 | handleResponse createForwardingNumber(client, '~', '~', |
||
| 184 | ENV['IVRDEMO_ROUTING_EXTENSION_DIRECT_NUMBER'], |
||
| 185 | ENV['IVRDEMO_ROUTING_EXTENSION_LABEL']) |
||
| 186 | end |
||
| 187 | |||
| 188 | if 1==0 |
||
| 189 | handleResponse updateAnsweringRuleOnRoutingExt(client, '~', '~', 'business-hours-rule', 12345678) |
||
| 190 | end |
||
| 191 | |||
| 192 | puts 'DONE' |
||
| 193 |