Jump to content
View in the app

A better way to browse. Learn more.

FMForums.com

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

How do I reference parameters within Groovy?

Featured Replies

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

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

  • Author

Worked like a charm! Thanks John!

Create an account or sign in to comment

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.