Pigeon Agents Development Demo Login Agent - DatasmithSA/Pigeon-Voice-Training GitHub Wiki
After creating a Pigeon Voice Agents library, follow the initial steps in creating a LUT agent, by adding a class, inheriting from the PVLutAgentBase class and overriding the base class' Process method.
N.B. keep in mind that a LUT message from the Voice Application on the device is synchronous i.e. the device blocks and waits for a response from the server before continuing with the dialog. Therefore every LUT agent is required to provide/add at least one output record to the response.
Add the following code to create the Login agent:
#region Using Directives
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Pigeon.Voice.SDK.AgentBase.TCP_IP;
using Pigeon.Voice.SDK.Sockets.LUT;
using Pigeon.Voice.SDK.Sockets;
using Pigeon.Voice.Demo.Agents.Data.DB;
#endregion //Using Directives
public class LutLoginAgent : PVLutAgentBase
{
#region Methods
public override void Process(PVLutResponse response)
{
try
{
GetInputParameters(response.Request, out string password);
AgentsDBContext.Create().Login(response.Request.OperatorId, password);
response.AddRecord(new List<string>() { 0.ToString(), string.Empty });
}
catch (Exception ex)
{
response.Records.Clear();
response.AddRecord(new List<string>() { 1.ToString(), ex.Message });
throw ex;
}
}
private void GetInputParameters(PVSocketRequest request, out string password)
{
if (request.InputRecordFieldValues.Count != 1)
{
throw new Exception(string.Format("Invalid number of inputs supplied to {0}.", request.CommandName));
}
password = request[0];
}
#endregion //Methods
}- Get input parameters: The LutLoginAgent starts by simply reading the input parameters, which in this case is only the password. This is done in the GetInputParameters method, by reading the input parameters from the Request i.e. a property of the PVLutResponse parameter on the agent's process method). The OperatorId property is provided in every request to an agent, hence the Voice Application does not explicitly send it as an input parameter. Instead the operator ID is retrieved from the Request.
-
Query database: an AgentsDBContext is created and the Login method is called by passing in the operator ID and password.
- If the operator ID and/or password is incorrect the AgentsDBContext's Login will throw an exception, in which case the Process method will catch it and add an output record with a result code of 1 and the result message being the exception's message. The device will interpret any result code other than 0 as an error and speak the result message to the operator.
- If the login method executes successfully, we then add an output record with a Result Code of 0 and an empty Result Message. This will indicate to the device a successful login.