How do i validate and give list of options after first wrong attempt in FormFlow Bot Framework - c#

I am trying to build a bot using bot framework where i want to take string from user for a department name using FormFlow and if user enters wrong department name, I want to validate and give back a list of choices to choose from
DepartmentName string:
[Prompt("What is your department name? {||}")]
public string DepartmentName { get; set; }
Deaprtment name field is as following:
.Field(nameof(DepartmentName),
validate: async (state, response) =>
{
var value = (string)response;
var result = new ValidateResult() { IsValid = false, Feedback = "Department name is not valid"};
if (Enum.GetNames(typeof(Department)).Any(x => x.ToLower() == value))
{
result.IsValid = true;
result.Feedback = null;
result.Value = value;
}
return result;
})
Department enum is as following:
public enum Department
{
hr = 1,
sales,
marketing,
development,
qm
}
How can i prompt user with list of departments in enum if first attempt goes wrong? Thnaks

1) you can just add the options to the end of the Feedback:
.Field(nameof(DepartmentName),
validate: (state, response) =>
{
var value = (string)response;
string[] departments = Enum.GetNames(typeof(Department)).ToArray();
var feedback = $"Department name is not valid. Options:\n\n {String.Join("\n\n", departments)}";
var result = new ValidateResult() { IsValid = false,
Feedback = feedback };
if (departments.Any(x => x.ToLower() == value))
{
result.IsValid = true;
result.Feedback = null;
result.Value = value;
}
return Task.FromResult<ValidateResult>(result);
});
2) you can use the Choice class (normally for disambiguation):
.Field(nameof(DepartmentName),
validate: (state, response) =>
{
var value = (string)response;
string[] departments = Enum.GetNames(typeof(Department)).ToArray();
IEnumerable<Choice> choices = departments.Select(d => new Choice()
{
Description = new DescribeAttribute(d, null, null, null, null),
Terms = new TermsAttribute() { Alternatives = new[] { d } },
Value = d
}).ToArray();
var result = new ValidateResult()
{
IsValid = false,
Choices = choices,
Feedback = "Department name is not valid."
};
if (departments.Any(x => x.ToLower() == value))
{
result.IsValid = true;
result.Feedback = null;
result.Value = value;
}
return Task.FromResult<ValidateResult>(result);
});

Related

How can I fix this Error: not all code paths return a value

My project has 2 part ClientSide and Server side. In Server side I have Controller that it needs query and command. I put both of them and my command has a handler but after I'd done my handler it throws a error that say: not all code paths return a value.
this is my handler:
public Task<ReturnCreateDataQuery> Handle(CreateCompletedActCommand request, CancellationToken cancellationToken)
{
var party = _dbContext.Parties.SingleOrDefault(t => t.PartyCode == request.PartyCode);
if (party == null) throw new Exception("");
if ((bool)request.ResonableType)
{
var departmentText = request.DepartmentIds.Any() ? string.Join(",", request.DepartmentIds.Distinct().OrderBy(t => t)) : string.Empty;
var cartableActs = new List<CartableActModel>();
var cartable = new CartableStateModel()
{
ClaimId = request.ClaimId,
CreationDate = DateTime.Now,
StatusCode = (int)Enumeration.StateType.Compeleted,
PreviouseCartableStateId = request.CartableStateId
};
var cartableAct = new CartableActCompleteModel()
{
ActCode = (int)Enumeration.ActType.CompleteCustomerData,
ActorId = party.PartyId,
CartableStateId = cartable.CartableStateId,
ChangeDate = DateTime.Now,
ClaimSubjectId = request.ClaimSubjectId,
ClaimType = request.ClaimType,
Departments = departmentText,
ExpertPartyId = request.ExpertPartyId,
ResonableType = request.ResonableType,
SubClaimSubjectId = request.SubClaimSubjectId,
CompletedDescription = request.CompletedDescription,
};
var attachments = request.Attachments.Select(t => new ActAttachmentModel
{
AttachmentContent = t.AttachmentContent,
ActAttachmentId = cartableAct.CartableActId,
ActId = cartableAct.CartableActId,
CreationDate = DateTime.Now,
Creator = party.PartyId,
FileExtension = t.FileExtension,
Title = t.Title,
MimeType = t.MimeType
}).ToList();
cartableAct.ActAttachments = attachments;
cartableActs.Add(cartableAct);
cartable.CartableActs = cartableActs;
_dbContext.Cartables.Add(cartable);
_dbContext.SaveChangesAsync(cancellationToken);
}
else
{
var cartableActs = new List<CartableActModel>();
var cartable = new CartableStateModel()
{
ClaimId = request.ClaimId,
CreationDate = DateTime.Now,
StatusCode = (int)Enumeration.StateType.Finished,
};
var cartableAct = new CartableActSatisficationModel()
{
ActCode = (int)Enumeration.ActType.SatisficationCustomer,
ActorId = party.PartyId,
CartableStateId = cartable.CartableStateId,
ChangeDate = DateTime.Now,
IsSatisfy = false,
SatisfyLevel = "1",
};
var attachments = request.Attachments.Select(t => new ActAttachmentModel
{
AttachmentContent = t.AttachmentContent,
ActAttachmentId = cartableAct.CartableActId,
ActId = cartableAct.CartableActId,
CreationDate = DateTime.Now,
Creator = party.PartyId,
FileExtension = t.FileExtension,
Title = t.Title,
MimeType = t.MimeType
}).ToList();
var outBox = new OutBoxModel
{
SentType = "SMS",
ClaimId = request.ClaimId,
IsSent = false,
PartyCode = request.PartyCode,
IsCustomer = true
};
cartableAct.ActAttachments = attachments;
cartableActs.Add(cartableAct);
cartable.CartableActs = cartableActs;
_dbContext.Cartables.Add(cartable);
_dbContext.OutBoxes.Add(outBox);
_dbContext.SaveChangesAsync(cancellationToken);
}
}
I don't know how can I fix this error I search a lot of source but I can't understand which value should return if you know this I would thank you.
Change your metod header
public async Task Handle(CreateCompletedActCommand request, CancellationToken cancellationToken)
{
....your code
}

Merge two Lists of same type with diff values and avoid duplicates

I have two lists of same type with different key value pairs,
List1 has "isPermanent = true" and List2 has false value and also
List1 has an extra key "nextVacationDate".
Im trying to do union of these as below but im afraid I will still get the duplicates because of different values. I need to merge both lists in to one list and order by List1 first (Permanent employees first)..is there a better way to do this using LINQ?
public newList1 List1(string abcd)
{
var result = serviceMethod1(abcd);
var newList1 = new List<emp>();
if (result == null) return null;
newList.AddRange(
result.Select(x => new Model
{
firstName = x.FName,
secondName = x.SName,
address = x.Address,
employeeId = x.EmpId,
isPermanent = true,
nextVacationDate =x.VacDt,
salary = x.Bsalary
}));
return newList1;
}
public newList2 List2(string defg)
{
var result = serviceMethod2(defg);
var newList2 = new List<emp>();
if (result == null) return null;
newList.AddRange(
result.Select(x => new Model
{
firstName = x.FName,
secondName = x.SName,
address = x.Address,
employeeId = x.EmpId,
isPermanent = false,
salary = x.Bsalary
}));
return newList2;
}
private List<emp> EmployyeList(List<emp> newList1, List<emp> newList2)
{
var sortedEmpList1 = newList1.OrderBy(i => i.Fname);
var sortedEmpList2 = newList2.OrderBy(i => i.Fname);
List<MeterModel> combinedList = newList1.Union(newList2) as List<emp>;
return combinedList;
}
You can filter the 2nd list to avoid duplicates:
newList1.Union(newList2.Where(emp2 => !newList1.Any(emp1 => emp1.employeeId == emp2.employeeId)))

default values for IEnumerable collection when length is zero

I have a IEnumerable collection:
public IEnumerable<Config> getConfig(string CID, string name)
{
var raw = db.ap_GetInfo(CID, name);
foreach (var item in raw.ToList().Where(x => x.Name!= null))
{
var x = raw.Count();
yield return new Config
{
Name = item.Name.ToString(),
Value = item.Value.ToString(),
};
}
}
The problem I am facing is that if this return a length of zero I am then unable to set the attributes to something else, If I have a response of length 1 the attributes are set from the database, however length zero I want to set a dfault value for Name and Value.
A LINQ solution - this returns the default if there are no items in the enumerable using DefaultIfEmpty:
public IEnumerable<Config> GetConfig(string CID, string name)
{
return db.ap_GetInfo(CID, name)
.Where(x => !string.IsNullOrEmpty(x.Name))
.Select(x => new Config
{
Name = x.Name.ToString(),
Value = x.Value.ToString(),
})
.DefaultIfEmpty(new Config
{
Name = "DefaultName",
Value = "DefaultValue"
});
}
If I understood your question correctly, you want to replace the case
0 results
with
1 result with a default value.
If that is correct, the easiest way is to fix this in the calling function:
var result = getConfig(...).ToList();
if (!result.Any())
{
result = new[] {new Config {Name = "DefaultName", Value = "DefaultValue"}};
}
Obviously, you can wrap this in a new function:
public IEnumerable<ClubConfig> getConfigOrDefault(string CID, string name)
{
var result = getConfig(CID, name).ToList();
if (result.Any())
return result;
else
return new[] {new Config {Name = "DefaultName", Value = "DefaultValue"}};
}
To check if your query did return any elements use Any-method.
public IEnumerable<ClubConfig> getConfig(string CID, string name)
{
var raw = db.ap_GetInfo(CID, name);
if (!raw.Any()) return new[] {
ClubConfig
{
Name = "defaultName",
Value = "defaultValue"
}};
foreach (var item in raw.Where(x => !string.IsNullOrEmpty(x.Name))
{
yield return new ClubConfig
{
Name = item.Name.ToString(),
Value = item.Value.ToString(),
};
}
}
EDIT: You can also omit the ToList from your input.
You can do this using LINQ and can maintain lazy evaluation of the IEnumerable result as follows:
public IEnumerable<ClubConfig> getConfig(string CID, string name)
{
var raw = db.ap_GetInfo(CID, name);
return raw.Where(x => !string.IsNullOrEmpty(x.Name))
.Select(item => new ClubConfig
{
Name = item.Name.ToString(),
Value = item.Value.ToString(),
})
.DefaultIfEmpty(new ClubConfig { Name = "n", Value="v" });
}

Task.WhenAll gives an error

public async virtual Task<ActionResult> Store(int? id, int? mainRoadID, int? segmentID, int? cityid, string serverMessage = "")
{
UserTrafficReport_Create model = await Task.WhenAll(GetNewModel, InitialCameras, GetMonitoredWaysListAndPushViewData(mainRoadID, segmentID, cityid));
return View(model);
}
The previous function has an error line ... I can't find the exact error
Error 1358 The best overloaded method match for 'System.Threading.Tasks.Task.WhenAll(params System.Threading.Tasks.Task[])' has some invalid arguments
And those are the three functions used in When All
public async virtual Task<UserTrafficReport_Create> GetNewModel(int? id, int? mainRoadID, int? segmentID, int? cityid)
{
var model = new UserTrafficReport_Create();
var serializer = new JavaScriptSerializer();
if (id != null && id > 0)
{
var report = _repository.FindOne<UserTrafficReport>((int)id);
model.InjectFrom(report);
model.Comments = report.Comments;
if (report.PictureSize.HasValue && report.PictureSize > 0)
model.photo_name = report.ID.ToString(CultureInfo.InvariantCulture);
if (report.RoadID != null)
{
model.RoadID = (int)report.RoadID;
_repository.FindOne<MonitoredWay>((int)report.RoadID);
}
FakeUsers(report.UserID);
model.RoadStatus = report.RoadStatus ?? 99;
if (report.traffic_rating >= 0)
model.traffic_rating = report.traffic_rating;
else
model.traffic_rating = null;
}
else
{
var fakeGroup = _repository.First<UserGroup>(g => g.Name.Contains("Fake"));
var fakeGroupId = 3;
if (fakeGroup != null)
fakeGroupId = fakeGroup.ID;
var dbNamesList = (from userAD in _context.UserAdditionalDatas
join groups in _context.UserMultiGroups on userAD.ID equals groups.UserDataId
join aspUser in _context.AspnetUsers on userAD.ID equals aspUser.ID
where (groups.UserGroupId == fakeGroupId)
select new
{
name = userAD.FirstName,
UserName = aspUser.Username,
userId = aspUser.ID
}).Distinct().ToList();
if (dbNamesList.Any())
{
var randomedList = dbNamesList.Randomize();
var fakeUser = randomedList.FirstOrDefault();
if (fakeUser != null)
{
model.GuestName = fakeUser.name;
model.UserID = fakeUser.userId;
}
}
model.RoadID = segmentID.GetValueOrDefault(-1);
model.traffic_rating = -1;
if (cityid != null)
model.CityId = (int)cityid;
}
return model;
}
.
public async virtual Task InitialCameras(int? cityid,string serverMessage = "")
{
var serializer = new JavaScriptSerializer();
var conditionslist = CreateListFromSingle(
new
{
value = "99",
text = "Not Specified"
}
);
conditionslist.Add(new { value = "4", text = "Accident" });
conditionslist.Add(new { value = "2", text = "Danger" });
conditionslist.Add(new { value = "3", text = "Road Work" });
string outputOfConditions = serializer.Serialize(conditionslist);
ViewData["ConditionsListSerialized"] = outputOfConditions;
var conditionslistitems =
(from condition in conditionslist
select new SelectListItem
{
Value = condition.value,
Text = condition.text
}).ToList();
ViewBag.ConditionsList = conditionslistitems;
ViewData["serverMsg"] = serverMessage;
if (cityid == null || cityid == -1)
{
var cameras = _context.Cameras.Select(c => new
{
value = c.Id,
text = c.Name
}).ToList();
cameras.Insert(0, new { value = (long)0, text = "--Select a Camera --" });
ViewData["Cameras"] = serializer.Serialize(cameras);
}
else
ViewData["Cameras"] = GetCityCameras((int)cityid);
}
..
private async Task GetMonitoredWaysListAndPushViewData(int? roadID = null, int? segmentID = null, int? cityID = null, Guid? reporterId = null)
{
int? id = cityID;
var dbWaysList =
_context.MonitoredWays.Where(
m =>
!m.IsTest &&
(m.RoadID != null && m.Road.AppOrder >= 0 && (id <= 0 || id == null)
? m.Road.AreaID > 0
: m.Road.AreaID == id));
var xWayseSelectList = (from s in dbWaysList
select new
{
OppId = s.OppositeSegment ?? 0,
Value = s.ID,
Title = s.EnglishName,
RoadTitle = s.Road.EnglishName
}).ToList().Distinct();
var repsList = (from s in xWayseSelectList//context.MonitoredWays
select new SelectListItem
{
Value = s.Value.ToString(CultureInfo.InvariantCulture),
Text = string.IsNullOrEmpty(s.RoadTitle) ? s.Title : s.RoadTitle + " (" + s.Title + ")",
Selected = segmentID != null && (segmentID.Value == s.Value)
}).Distinct().ToList();
var serializer = new JavaScriptSerializer();
string wayseSelectListOppId = serializer.Serialize(xWayseSelectList);
string outputOfAreas = serializer.Serialize(repsList);
ViewData["MonitoredWaysListSerialized"] = outputOfAreas;
ViewData["OppositeMonitoredWays"] = wayseSelectListOppId;
ViewBag.MonitoredWaysList = repsList;
var conditionslist = CreateListFromSingle(
new
{
value = "99",
text = "Not Specified"
}
);
conditionslist.Add(new { value = "4", text = "Accident" });
conditionslist.Add(new { value = "2", text = "Danger" });
conditionslist.Add(new { value = "3", text = "Road Work" });
string outputOfConditions = serializer.Serialize(conditionslist);
ViewData["ConditionsListSerialized"] = outputOfConditions;
var conditionslistitems =
(from condition in conditionslist
select new SelectListItem
{
Value = condition.value,
Text = condition.text
}).ToList();
ViewBag.ConditionsList = conditionslistitems;
var ratingslist = CreateListFromSingle(
new
{
value = "0",
text = "V. Bad"
}
);
ratingslist.Add(new { value = "1", text = "Bad" });
ratingslist.Add(new { value = "2", text = "Average" });
ratingslist.Add(new { value = "3", text = "Good" });
ratingslist.Add(new { value = "3", text = "V. Good" });
ViewBag.Ratingslist = ratingslist;
string outputOfRatings = serializer.Serialize(ratingslist);
ViewData["RatingsListSerialized"] = outputOfRatings;
if (roadID != null)
{
var rod = _context.Roads.FirstOrDefault(r => r.ID == roadID);
if (rod != null)
{
cityID = rod.AreaID;
}
}
var dbAreassList = _context.Cities.ToList();
var areas =
(from area in dbAreassList
select new SelectListItem
{
Value = area.ID.ToString(CultureInfo.InvariantCulture),
Text = area.EnglishName,
Selected = cityID != null && (cityID.Value == area.ID)
}).ToList();
ViewBag.AreasList = areas;
var areasList = (from s in _context.Cities
select
new
{
id = s.ID,
text = s.EnglishName
}).ToList();
serializer = new JavaScriptSerializer();
string outputOfAreas1 = serializer.Serialize(areasList);
ViewData["AreasListSerialized"] = outputOfAreas1;
var fakeGroup = _repository.First<UserGroup>(g => g.Name.Contains("Fake"));
var fakeGroupId = 3;
if (fakeGroup != null)
fakeGroupId = fakeGroup.ID;
var dbNamesList = (from userAD in _context.UserAdditionalDatas
join groups in _context.UserMultiGroups on userAD.ID equals groups.UserDataId
join aspUser in _context.AspnetUsers on userAD.ID equals aspUser.ID
where (groups.UserGroupId == fakeGroupId)
select new
{
Text = userAD.FirstName,
Value = userAD.ID,
Selected = false
//Email = aspUser.Username
}).Distinct().ToList();
var namess = dbNamesList.Select(s => new SelectListItem
{
Text = s.Text,
Value = s.Value.ToString(),
Selected = s.Selected
}).ToList();
if (reporterId != null)
{
var member = _repository.FindOne<UserAdditionalData>((Guid)reporterId);
if (member != null)
{
namess.Add(new SelectListItem
{
Text = member.FirstName,
Value = member.ID.ToString(),
Selected = true
});
}
}
var random = new Random();
if (!namess.Any(n => n.Selected))
{
int rand = random.Next(0, namess.Count - 1);
namess[rand].Selected = true;
}
ViewBag.FakeUsersList = namess;
}
A few things wrong with this line:
UserTrafficReport_Create model =
await Task.WhenAll(
GetNewModel,
InitialCameras,
GetMonitoredWaysListAndPushViewData(mainRoadID, segmentID, cityid));
Task.WhenAll takes a collection of Task instances as an argument.
You're passing 2 delegates and a task. You probably meant to actually call the first two methods, so that they'll return a task?
Task.WhenAll returns a Task. Awaiting that task won't return anything, so you won't be able to assign anything to model.
Task<UserTrafficReport_Create> modelFactoryTask = GetNewModel(...);
await Task.WhenAll(
modelFactoryTask ,
InitialCameras(...),
GetMonitoredWaysListAndPushViewData(mainRoadID, segmentID, cityid));
UserTrafficReport_Create model = modelFactoryTask.Result;

Writing to incidents in C#

I am using CRM 4 and the SDK to grab cases like so:
public List<Case> GetCases()
{
List<Case> cases = new List<Case>();
#region Retrieve Resolved Cases
try
{
InitSession();
RetrieveMultipleRequest req = new RetrieveMultipleRequest();
req.ReturnDynamicEntities = true;
//QueryExpression says what entity to retrieve from, what columns we want back and what criteria we use for selection
QueryExpression qe = new QueryExpression();
qe.EntityName = EntityName.incident.ToString();
List<string> attributes = new string[] {
"incidentid","title" ,"description", "ticketnumber", "statuscode",
"kez_allocatedhours",
"customerid",
"casetypecode"
}.ToList();
//columns to retireve
ColumnSet AvailabilityColumnSet = new ColumnSet();
AvailabilityColumnSet.Attributes = attributes.ToArray();
qe.ColumnSet = AvailabilityColumnSet;
//filter
FilterExpression fe = new FilterExpression();
fe.FilterOperator = LogicalOperator.And;
//condtion for filter
ConditionExpression isResolved = new ConditionExpression();
isResolved.AttributeName = "statuscode";
isResolved.Operator = ConditionOperator.NotEqual;
isResolved.Values = new string[] { "5" };
fe.Conditions = new ConditionExpression[] { isResolved }; //Add the conditions to the filter
qe.Criteria = fe; //Tell the query what our filters are
req.Query = qe; //Tell the request the query we want to use
//retrieve entities
RetrieveMultipleResponse resp = svc.Execute(req) as RetrieveMultipleResponse;
if (resp != null)
{
BusinessEntity[] rawResults = resp.BusinessEntityCollection.BusinessEntities;
List<DynamicEntity> castedResults = rawResults.Select(r => r as DynamicEntity).ToList();
foreach (DynamicEntity result in castedResults)
{
string id = GetProperty(result, "incidentid");
string title = GetProperty(result, "title");
string description = GetProperty(result, "description");
string ticket = GetProperty(result, "ticketnumber");
string customer = GetProperty(result, "customerid");
int statuscode = -1;
string statusname = "";
double estHours = 0.0;
string casetype = "";
int casetypecode = -1;
Property prop = result.Properties.Where(p => p.Name == "statuscode").FirstOrDefault();
if (prop != null)
{
StatusProperty status = prop as StatusProperty;
if (status != null)
{
statuscode = status.Value.Value;
statusname = status.Value.name;
}
}
prop = result.Properties.Where(p => p.Name == "kez_allocatedhours").FirstOrDefault();
if (prop != null)
{
CrmFloatProperty fl = prop as CrmFloatProperty;
if (fl != null)
{
estHours = fl.Value.Value;
}
}
prop = result.Properties.Where(p => p.Name == "casetypecode").FirstOrDefault();
if (prop != null)
{
PicklistProperty fl = prop as PicklistProperty;
if (fl != null)
{
casetype = fl.Value.name;
casetypecode = fl.Value.Value;
}
}
Case c = new Case();
c.ID = id;
c.Title = title;
c.Description = description;
c.StatusCode = statuscode;
c.StatusName = statusname;
c.TicketNumber = ticket;
c.CustomerName = customer;
c.EstimatedHours = estHours;
c.Type = casetype;
c.TypeCode = casetypecode;
bool allowedThroughStat = true;
bool allowedThroughType = true;
var userStatuses = SettingsManager.Get("CRMUserStatusReasons").Split(';').ToList().Where(p => p.Length > 0).ToList();
var userTypes = SettingsManager.Get("CRMUserCaseTypes").Split(';').ToList().Where(p => p.Length > 0).ToList();
if(userStatuses.Count > 0 && !userStatuses.Contains(c.StatusCode.ToString()))
{
allowedThroughStat = false;
}
if (userTypes.Count > 0 && !userTypes.Contains(c.TypeCode.ToString()))
{
allowedThroughType = false;
}
if(allowedThroughStat && allowedThroughType)
cases.Add(c);
}
}
}// end try
catch (Exception)
{
return null;
// The variable 'e' can access the exception's information.
// return "Error Message: " + e.Message.ToString() + " | Stack Trace: " + e.StackTrace.ToString();
}
return cases;
#endregion
}
However, now I need to be able to change the status and title of a case from C# given its incidentid.
Ive looked at the SDK docs and cannot find an example of this.
Anyone work with this before?
Thanks
Simply put, above is code to read an incident. Could I get an example of writing an incident field, Just one. Ex: How could I change the title of an incident.
You can call the Update method on the CrmService. Here is the SDK article.
Case c = new Case();
c.ID = id;
c.Title = title;
svc.Update(c);
To change the state of an entity you use the setstaterequest. If you want to do it to a dynamic entity there's a description in this blog

Categories

Resources