January 24, 201312 yr Hello, This is likely a simple question, but Groovy is not my normal language. I have a script to send SMS. I can get it to work using strings. But when I replace the string with the incoming parameter name, I receive an error. My incoming parameters are defined as follows: Parameters: {to=+15035551234, from=(503) 555-1111, message=testing} Here is my script: // Download the twilio-java library from http://twilio.com/docs/libraries import java.util.Map; import java.util.HashMap; import com.twilio.sdk.resource.instance.Account; import com.twilio.sdk.TwilioRestClient; import com.twilio.sdk.TwilioRestException; import com.twilio.sdk.resource.factory.SmsFactory; import com.twilio.sdk.resource.instance.Sms; public class SmsSender { /* Find your sid and token at twilio.com/user/account */ public static final String ACCOUNT_SID = "xxx"; public static final String AUTH_TOKEN = "yyy"; public static void main(String[] args) throws TwilioRestException { TwilioRestClient client = new TwilioRestClient(ACCOUNT_SID, AUTH_TOKEN); Account account = client.getAccount(); SmsFactory smsFactory = account.getSmsFactory(); Map<String, String> smsParams = new HashMap<String, String>(); smsParams.put("To", to); smsParams.put("From", from); // Replace with a valid phone // number in your account smsParams.put("Body", message); Sms sms = smsFactory.create(smsParams); } } The parameters are meant to be used in these lines: smsParams.put("To", to); smsParams.put("From", from); smsParams.put("Body", message); This is the error I receive: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: Script1.groovy: 25: Apparent variable 'to' was found in a static scope but doesn't refer to a local variable, static field or class. Possible causes: You attempted to reference a variable in the binding or an instance variable from a static context. You misspelled a classname or statically imported field. Please check the spelling. You attempted to use a method 'to' but left out brackets in a place not allowed by the grammar. @ line 25, column 29. smsParams.put("To", to); ^ Script1.groovy: 26: Apparent variable 'from' was found in a static scope but doesn't refer to a local variable, static field or class. Possible causes: You attempted to reference a variable in the binding or an instance variable from a static context. You misspelled a classname or statically imported field. Please check the spelling. You attempted to use a method 'from' but left out brackets in a place not allowed by the grammar. @ line 26, column 31. smsParams.put("From", from); // Replace with a valid phone ^ Script1.groovy: 28: Apparent variable 'message' was found in a static scope but doesn't refer to a local variable, static field or class. Possible causes: You attempted to reference a variable in the binding or an instance variable from a static context. You misspelled a classname or statically imported field. Please check the spelling. You attempted to use a method 'message' but left out brackets in a place not allowed by the grammar. @ line 28, column 31. smsParams.put("Body", message); ^ 3 errors I really need to know how to reference the parameters within the script. I have done this before but not within a class structure. Thanks, Jeff
January 24, 201312 yr Cant test his obviously without an account.... but if you replace everything after the imports this might work... ACCOUNT_SID = 'xxx' AUTH_TOKEN = 'yyy' try{ client = new TwilioRestClient(ACCOUNT_SID, AUTH_TOKEN) account = client.getAccount() smsFactory = account.getSmsFactory() smsParams = [:] smsParams.put('To', to) smsParams.put('From', from) // Replace with a valid phone number in your account smsParams.put('Body', message) sms = smsFactory.create(smsParams) } catch (e) {return e} return sms.getSid() and shouldn't need the first two import lines... John
Create an account or sign in to comment