CloudCherry is now part of Cisco.
Learn More About Cisco

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.

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.

Store the Access Token received from Authentication API and use it for next 24hours to create survey links dynamically. Pass the required prefill information while creating token

``` 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

https://api.getcloudcherry.com/api/Download/{code}  

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;
        }
    }
}