API: Invite participants to a survey

More
1 year 9 months ago #149690 by jltrussart11
jltrussart11 created the topic: API: Invite participants to a survey
Hi,


This php json script works well to add a participant to the survey 318848.
I'm trying to automatically send the invitation at the end.
Any idea why this routine does'nt work?

Thanks for your precious help!



require_once 'application/libraries/jsonRPCClient.php';

define( 'LS_BASEURL', ' www.vecteursleviers.com/conseil ');
define( 'LS_USER', '***' );
define( 'LS_PASSWORD', '***' );
define( 'LS_DB_HOST', 'localhost' );
define( 'LS_DB_NAME', 'limesurvey' );
define( 'LS_DB_USER', 'root' );
define( 'LS_DB_PASS', '' );
define( 'LS_DB_TABLEPREFIX', 'lime_' );

$iSurveyID = 318848;
$token = 'ABCDE';
$email = 'This email address is being protected from spambots. You need JavaScript enabled to view it.';
$LastNameAPI = 'Nom famille' ;
$FirstNameAPI = 'Prenom' ;


// Instantiate a new RPC client
$myJSONRPCClient = new jsonRPCClient( LS_BASEURL.'/admin/remotecontrol' );

// Get a session key
$sSessionKey= $myJSONRPCClient->get_session_key( LS_USER, LS_PASSWORD );

// Define the token params
$tokenParams = array("email"=>$email,"lastname"=>$LastNameAPI,"firstname"=>$FirstNameAPI,"token"=>$token,"language"=>'fr',"emailstatus"=>"OK");
$aParticipantData=array($tokenParams);
$bCreateToken = true;

// Create the tokens
$newToken = $myJSONRPCClient->add_participants( $sSessionKey, $iSurveyID, $aParticipantData, $bCreateToken);

// Print returned results
echo '<hr><br><h1>Limesurvey</h1><br>New token created in survey '.$iSurveyID.':'
.'<ul>'
.'<li>TID - '.$newToken[0].'</li>'
.'<li>Token - '.$newToken[0].'</li>'
.'</ul>';

// Send Invitations routine: invite_participants(string $sSessionKey, int $iSurveyID, array $aTokenIds = false, bool $bEmail = true) : array
invite_participants(string $sSessionKey, int $iSurveyID, array $aTokenIds = $newToken, bool $bEmail = true) : array

// Release the session key
$myJSONRPCClient->release_session_key( $sSessionKey );
?>

Please Log in or Create an account to join the conversation.

More
1 year 9 months ago - 1 year 9 months ago #149782 by tpartner
tpartner replied the topic: API: Invite participants to a survey
This will create a token and send an invitation to that token:

<?php
 
	require_once 'application/libraries/jsonRPCClient.php';
 
	define( 'LS_BASEURL', ' www.pathTo/limesurvey ');
	define( 'LS_USER', '***' );
	define( 'LS_PASSWORD', '***' );
 
	$iSurveyID = 318848;
	$token = 'ABCDE';
	$email = 'example@example.com';
	$LastNameAPI = 'Nom famille' ;
	$FirstNameAPI = 'Prenom' ;
 
 
	// Instantiate a new RPC client
	$myJSONRPCClient = new jsonRPCClient( LS_BASEURL.'/admin/remotecontrol' );
 
	// Get a session key
	$sSessionKey= $myJSONRPCClient->get_session_key( LS_USER, LS_PASSWORD );
 
	// Define the token params
	$tokenParams = array("email"=>$email,"lastname"=>$LastNameAPI,"firstname"=>$FirstNameAPI,"token"=>$token,"language"=>'fr',"emailstatus"=>"OK");
	$aParticipantData=array($tokenParams);
	$bCreateToken = true;
 
	// Create the tokens
	$newToken = $myJSONRPCClient->add_participants( $sSessionKey, $iSurveyID, $aParticipantData, $bCreateToken);
 
	// Print returned results
	echo '<hr><br><h1>Limesurvey</h1><br>New token created in survey '.$iSurveyID.':'
	.'<ul>'
	.'<li>TID - '.$newToken[0]['tid'].'</li>'
	.'<li>Token - '.$newToken[0]['token'].'</li>'
	.'</ul>';
 
	// Send an invitation to the created token
	$tokenIDs = array($newToken[0]['tid']);
	$newMail = $myJSONRPCClient->invite_participants($sSessionKey, $iSurveyID, $tokenIDs, true);
 
	// Print returned results
	if($newMail[$newToken[0]['tid']]['status'] == 'OK') {
		echo 'Invitation sent to:'
		.'<ul>'
		.'<li>Name - '.$newMail[$newToken[0]['tid']]['name'].'</li>'
		.'<li>Email - '.$newMail[$newToken[0]['tid']]['email'].'</li>'
		.'</ul>';
	}
	else {
		echo 'Error - no invitation sent!';
	}
 
	// Release the session key
	$myJSONRPCClient->release_session_key( $sSessionKey );
?>

Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Last Edit: 1 year 9 months ago by tpartner.
The following user(s) said Thank You: Ben_V, LouisGac, jltrussart11

Please Log in or Create an account to join the conversation.

More
1 year 9 months ago #149803 by jltrussart11
jltrussart11 replied the topic: API: Invite participants to a survey
Great! Works like a charm! Thanks a lot for your precious help!

Please Log in or Create an account to join the conversation.

More
1 year 6 months ago #155257 by first
first replied the topic: API: Invite participants to a survey

Survey Designer and Programmer

Please Log in or Create an account to join the conversation.

More
1 year 6 months ago #155269 by tpartner
tpartner replied the topic: API: Invite participants to a survey
Such a demand for help in another post is unlikely to get you any.

Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.

Please Log in or Create an account to join the conversation.

More
1 year 6 months ago #155270 by first
first replied the topic: API: Invite participants to a survey
Hi Tony - Thanks for reply. Just wanted to link related posts. Is this not ok?

Survey Designer and Programmer

Please Log in or Create an account to join the conversation.

More
1 year 5 months ago #156157 by orenh
orenh replied the topic: API: Invite participants to a survey
Hi,

I am using the invite, and it works but instead of sending the invite to one token its sends it to all the tokens in the survey...

I send to the api the session, the survey id and an array of tids, and it still sends the invite to all the tokens.
How do I make the api send the invite to only one token?

Thank you.

Please Log in or Create an account to join the conversation.

More
1 year 5 months ago #156187 by tpartner
tpartner replied the topic: API: Invite participants to a survey
The API should only send invitations to the token IDs that you provide in the $aTokenIds argument - api.limesurvey.org/classes/remotecontrol..._invite_participants

Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.

Please Log in or Create an account to join the conversation.

More
1 year 5 months ago #156191 by orenh
orenh replied the topic: API: Invite participants to a survey
Well, I know the API should send to only the ids I tell him, but it actually sends to all the tokens.

Here is the scenario:
1. I created a survey
2. Added 5 new tokens to the survey
3. Using the API I wanted to send an invite only to one of the tokens: here is the code:

// Instantiate a new RPC client
$oRPCClient = new jsonRPCClient( LIME_URL );

// Get a session key
$sessionKey= $oRPCClient->get_session_key( LIME_USER, LIME_PASSWORD );

// Send an invitation the selected token
$surveyID = 751954
$tIDs = array(38);
$res = $oRPCClient->invite_participants($sessionKey, $surveyID, $tIDs, true);


The result of this API call is that it actually sends the invites to all the 5 tokens.

The version of LimeSurvey: Version 2.6.4-lts

Please Log in or Create an account to join the conversation.

More
1 year 5 months ago #156203 by tpartner
tpartner replied the topic: API: Invite participants to a survey
This works for me and only sends to token ID 38 (not token value 38!).

<?php
 
	require_once 'jsonRPCClient.php';
 
	define( 'LS_BASEURL', 'http://pathTo/limeSurvey/');  
	define( 'LS_USER', 'admin' );
	define( 'LS_PASSWORD', 'password' );
 
	$iSurveyID = 123456;
 
 
	// Instantiate a new RPC client
	$myJSONRPCClient = new jsonRPCClient( LS_BASEURL.'/admin/remotecontrol' );
 
	// Get a session key
	$sSessionKey= $myJSONRPCClient->get_session_key( LS_USER, LS_PASSWORD );
 
	// Send an invitation to a token
	$tokenIDs = array(38);
	$newMail = $myJSONRPCClient->invite_participants($sSessionKey, $iSurveyID, $tokenIDs, true);
 
	// Release the session key
	$myJSONRPCClient->release_session_key( $sSessionKey );
?>

Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.

Please Log in or Create an account to join the conversation.

More
1 year 5 months ago #156374 by first
first replied the topic: API: Invite participants to a survey
My findings:
Same code works fine in : Version 2.67.1+170626
But send emails to all in : 2.06lts.

@Orenh: Can you log a bug?

Survey Designer and Programmer

Please Log in or Create an account to join the conversation.

More
1 year 5 months ago #156380 by first
first replied the topic: API: Invite participants to a survey
On further review I realized that it is also not a bug . Instead it seems to be additional feature with latest version of LS.

2.06lts:


latest ls:


Not survey if they are adding or removing features in lts version.

Survey Designer and Programmer
Attachments:
The following user(s) said Thank You: tpartner

Please Log in or Create an account to join the conversation.

More
4 months 3 weeks ago #171451 by davebostockgmail
davebostockgmail replied the topic: API: Invite participants to a survey
Hi Tony

I know this is a year old ... I have been trying this script as we now have a need to do this and it creates the entry into the token field but then refuses to send the email ... any thoughts?

Using 2.6.4 lts

Any hints or advice most welcome

Dave

Please Log in or Create an account to join the conversation.

More
4 months 3 weeks ago #171465 by tpartner
tpartner replied the topic: API: Invite participants to a survey
Can you send invitations through the LS GUI?

What is returned by $newMail?

If you see the error message, try replacing

echo 'Error - no invitation sent!';

with:

print_r($newMail);

or

echo $newMail;

Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.

Please Log in or Create an account to join the conversation.

More
4 months 3 weeks ago - 4 months 3 weeks ago #171480 by davebostockgmail
davebostockgmail replied the topic: API: Invite participants to a survey
So I did that and this is what I get as an output

New token created in survey 146297:
TID - 23
Token - B4ULIxdu2CpvDi7
Array ( [23] => Array ( [name] => Test Drive Customer => test@test.com [status] => fail [error] => Message body empty SMTP debug output: ) [status] => 0 left to send )[email] => This email address is being protected from spambots. You need JavaScript enabled to view it. [status] => fail [error] => Message body empty
SMTP debug output:
) [status] => 0 left to send )
Last Edit: 4 months 3 weeks ago by davebostockgmail.

Please Log in or Create an account to join the conversation.

Start now!

Just create your account and start using Limesurvey today.

Register now
Join our Newsletter!