import groovy.json.JsonOutput
import groovy.json.JsonSlurper
import javax.crypto.spec.SecretKeySpec
import javax.crypto.Mac
import java.net.URL
def smsData = [
"phone": "54999999999",
"message": "hello!",
"empresa": "Mobile Mind",
"email": "ricardo@mobilemind.com.br"
]
def jsonData = JsonOutput.toJson(smsData)
def smsAppId = "5a0f04f4da6ff0a99491ece1"
def smsServerEndpoint = "http://smssender.mobilemind.com.br/api/sms"
def smsAppKey = "abf950c6ac2b07854963dd4242e581a2923024ef9b0e6ab9011c01d23f840ac4"
def jsonHex = jsonData.bytes.encodeHex()
SecretKeySpec signingKey = new SecretKeySpec(smsAppKey.bytes, "HmacSHA1");
Mac mac = Mac.getInstance("HmacSHA1");
mac.init(signingKey);
// compute the hmac on input data bytes
byte[] rawHmac = mac.doFinal("${jsonHex}".bytes);
def calculedSignature = rawHmac.encodeBase64()
println calculedSignature
def baseUrl = new URL(smsServerEndpoint)
def connection = baseUrl.openConnection()
connection.addRequestProperty("Content-Type", "application/json")
connection.addRequestProperty("X-Hub-Application", smsAppId)
connection.addRequestProperty("X-Hub-Signature", "${calculedSignature}")
connection.with {
doOutput = true
requestMethod = 'POST'
outputStream.withWriter { writer ->
writer << jsonData
}
def jsonSlurper = new JsonSlurper()
def result = jsonSlurper.parseText(content.text)
println content.text
if(result["status"] == "success")
println result["id"] // sms id
}