Bulk survey token Creation
Content Outline
Bulk survey token creation
Survey token creation can be done in a couple of ways. You may either, create single surveys tokens or create them in bulk.
-
Single survey tokens are created when the volumes aren’t large. Only 15 API calls per second are allowed from a single IP. This is a rate limit imposed to avoid DDOS attacks.
-
Bulk Survey token creation is used when a large number of surveys are needed to be created in a short time.
Let’s explain the use of bulk token creation using a use case. Let’s consider a scenario where an integration with a Marketing automation tool is being built. You would like to embed Webex Experience Management surveys into a marketing mailer going out to millions of your customers.
In this case, you would be required to use the bulk survey token creation API and not the real-time survey token creation API.
Following steps are to be followed to create tokens in bulk.
-
Step 1: Call Webex Experience Management Login API to get authorization token
-
Step 2: Use Authorization token to call Bulk survey token API
``` https://api.getcloudcherry.com/api/SurveyByToken/Import/{days}/{uses}/{location}/{deliveryworkflow} ```
csvdata – Filetype (CSV data) through postman or a csvstring (stringbuilder)
days – Number of days the survey link will be valid for. Ideal number of days that we suggest is ‘7’
uses – Number of submissions that can be made with the survey link. The ideal number of submissions should be “1” as it’ll eliminate duplicate feedbacks given by the same customers
location – The store where the customers have purchased
deliveryworkflow – If delivery plan is created then pass that ID here
- Step 3: Use the output code of bulk import survey token to call download API
https://api.getcloudcherry.com/api/Download/{code}
- Step 4: Use the output string from download API to get the list of survey URLs, embed the survey links in Email body and send out email surveys
C# Bulk Token Creation
using System;
using System.IO;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
namespace TokenCreation
{
public class BulkTokenCreation
{
public async Task<string> CreateBulkToken(string accessToken)
{
//Survey Validity Days
int days = 30;
// Number of uses
int uses = 10;
// Touchpoint. CC Questionnaire name
string location = "Branch";
//Base URL
string endPoint = "https://api.getcloudcherry.com";
//URL to Create Token
string url = "/api/SurveyByToken/Import/" + days + "/" + uses + "/" + location;
try
{
//Read data from csv file. Sample data format to be downloade from CC product.
var csvdata = File.ReadAllText("SampleToken.csv");
//Composing Header
var fileContent = new StringContent(csvdata);
string filename = "csvtokens.csv";
fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
{
Name = "\"files\"",
FileName = "\"" + filename + "\""
}; // the extra quotes are key here
fileContent.Headers.ContentType = new MediaTypeHeaderValue("text/csv");
var content = new MultipartFormDataContent();
content.Add(fileContent);
HttpResponseMessage response = null;
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, endPoint + url);
request.Content = content;
request.Headers.Add("Authorization", "Bearer " + accessToken);
//Getting Uploaded file name from response
var httpClient = new HttpClient();
response = await httpClient.SendAsync(request);
if (response.IsSuccessStatusCode)
{
// Second step of the code is to download the file within 5 seconds
// Else the URL file will be deleted from server
//Download file
string apifile = await response.Content.ReadAsStringAsync();
if (!string.IsNullOrEmpty(apifile) && apifile.Contains("download/"))
{ // Return Filled CSV
HttpResponseMessage fileResponse = null;
var _url = endPoint + "/api/" + apifile.Replace("\"", "");
HttpRequestMessage fileRequest = new HttpRequestMessage(HttpMethod.Get, _url);
fileResponse = await httpClient.SendAsync(fileRequest);
byte[] bytestr = null;
if (fileResponse != null && fileResponse.IsSuccessStatusCode)
bytestr = await fileResponse.Content.ReadAsByteArrayAsync();
// Convert byte to string. Final output in string format which contains CSV data
// along with Survey Token and Survey URL as extra colums.
if (bytestr != null)
return Encoding.UTF8.GetString(bytestr, 0, bytestr.Length);
}
}
}
catch (Exception)
{
return null;
}
return null;
}
}
}