-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFetchAudioPrompt.cs
145 lines (115 loc) · 5.36 KB
/
FetchAudioPrompt.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
using NLog;
using PureCloudPlatform.Client.V2.Api;
using PureCloudPlatform.Client.V2.Client;
using PureCloudPlatform.Client.V2.Model;
using System.Diagnostics;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Microsoft.Extensions.Configuration;
using ShellProgressBar;
using System.Text;
namespace CallFlowVisualizer
{
// v1.5.0
internal class FetchAudioPrompt
{
internal static NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger();
internal static List<GenesysCloudFlowNode> FetchPromptDescription(List<GenesysCloudFlowNode> flowNodesList,Options opt)
{
var configRoot = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile(path: "appsettings.json").Build();
int maxSecondDescriptionLengh = configRoot.GetSection("cfvSettings").Get<CfvSettings>().MaxSecondDescriptionLengh;
ArchitectApi gcArchApi = new();
if (gcArchApi.Configuration.AccessToken == null)
{
FetchGCAccessToken.GetAccessToken(opt.profile);
}
var page = 1;
int pageSize = configRoot.GetSection("gcSettings").Get<GcSettings>().PageSize;
int pageCount;
PromptEntityListing promptEntityListing = new PromptEntityListing();
string br = "<br>";
foreach (var HasMetaflowNode in flowNodesList.Where(x => x.AudioMetaData.Count > 0))
{
string promptEntity = null;
string description = null;
string promptName = null;
StringBuilder descriptionBuilder = new StringBuilder();
int seq = 1;
foreach (var HasMetaflowNode_each in HasMetaflowNode.AudioMetaData)
{
if (HasMetaflowNode_each.Contains("Prompt."))
{
try
{
Logger.Info($"Fetch Prompt Description Entities Page:{page}");
promptName = HasMetaflowNode_each.Replace("Prompt.", "");
List<string> names = new List<string>() { promptName };
promptEntityListing = gcArchApi.GetArchitectPrompts(pageNumber: 1, name: names);
promptEntity = promptEntityListing.Entities.Select(e => e.Description).FirstOrDefault();
}
catch (Exception e)
{
Debug.Print("Exception when calling Architect.prompts: " + e.Message);
ColorConsole.WriteError("Exception when calling Architect.prompts: " + e.Message);
ColorConsole.WriteError("Updating prompt details has been aborted due to an error.");
return flowNodesList;
}
if (String.IsNullOrEmpty(promptEntity))
{
promptEntity = seq.ToString()+":Prompt "+ promptName;
}
else
{
promptEntity = seq.ToString() + ":Prompt " + promptEntity;
}
}
else
{
promptEntity = seq.ToString() + ":" + HasMetaflowNode_each;
}
if (descriptionBuilder.Length > 0)
{
descriptionBuilder.Append(br);
}
descriptionBuilder.Append(promptEntity);
seq++;
}
description = descriptionBuilder.ToString();
if (description.Length > maxSecondDescriptionLengh)
{
var descSplit = description.Split(new string[] { "<br>" }, StringSplitOptions.None);
var newDescription = new StringBuilder();
int remainingLength = maxSecondDescriptionLengh;
int avgLengthPerLine = remainingLength / descSplit.Length;
foreach (var desc in descSplit)
{
if (desc.Length <= avgLengthPerLine)
{
newDescription.Append(desc + "<br>");
remainingLength -= desc.Length;
}
else
{
string truncatedDesc = desc.Substring(0, avgLengthPerLine);
newDescription.Append(truncatedDesc + "<br>");
remainingLength -= truncatedDesc.Length;
}
if (remainingLength <= 0)
{
break;
}
}
description = newDescription.ToString();
}
HasMetaflowNode.Desc2 = description;
var itemToUpdate = flowNodesList.FirstOrDefault(x => x.Id == HasMetaflowNode.Id);
if (itemToUpdate != null)
{
itemToUpdate.Desc2 = description;
}
}
Logger.Info($"Fetch Prompt Description done");
return flowNodesList;
}
}
}