Jump to content
Claris Engage 2025 - March 25-26 Austin Texas ×

This topic is 4332 days old. Please don't post here. Open a new topic instead.

Recommended Posts

Posted

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

Posted

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

This topic is 4332 days old. Please don't post here. Open a new topic instead.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.