Created
November 18, 2015 10:13
-
-
Save horie1024/92ed61b69f051c7a5400 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async = require 'async' | |
Soracom = require 'soracom' | |
module.exports = (robot) -> | |
speed = | |
minimum: 's1.minimum' | |
slow: 's1.slow' | |
standard: 's1.standard' | |
fast: 's1.fast' | |
chk_speed = (target, speedObj) -> | |
if target == "min" | |
target = "minimum" | |
else if target == "std" | |
target = "standard" | |
speedObj[target] | |
robot.hear /soracom\s*android\s*speed\s*(minimum|min|slow|standard|std|fast)/i, (msg) -> | |
msg.send "SORACOM air SIMの速度クラスを変更します。" | |
soracom = new Soracom { | |
apiKey: process.env.SORACOM_API_KEY, | |
token: process.env.SORACOM_API_TOKEN, | |
operatorId: process.env.SORACOM_API_OPERATOR_ID} | |
async.waterfall [ | |
(callback) -> | |
soracom.get "/subscribers", | |
{tag_name: 'os', tag_value: 'android'}, (err, res, sims) -> | |
if err? | |
callback err, null | |
else | |
callback null, sims | |
(sims, callback) -> | |
speed_type = chk_speed(msg.match[1], speed) | |
if speed_type? | |
for sim in sims | |
path = "/subscribers/" + sim.imsi + "/update_speed_class" | |
soracom.post path, | |
{imsi: sim.imsi, speedClass: speed_type}, | |
(err, res, body) -> | |
if err? | |
callback err, null | |
else | |
callback null, "速度クラスを " + body.type + " に変更しました。" | |
else | |
callback("存在しない速度クラスです。", null) | |
], (err, done) -> | |
if err? | |
msg.send "エラーが発生しました。" | |
msg.send "エラー内容: " + err | |
else | |
msg.send done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment