Skip to content

Instantly share code, notes, and snippets.

@xximjasonxx
Created June 4, 2024 01:57
Show Gist options
  • Save xximjasonxx/5a994b46adbb8785c1b64b6d12ca2f3e to your computer and use it in GitHub Desktop.
Save xximjasonxx/5a994b46adbb8785c1b64b6d12ca2f3e to your computer and use it in GitHub Desktop.
[Function("HandleCallEventFunction")]
public async Task<HandleEventResponseModel> HandleEvent(
[HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "handle/event")] HttpRequestData request)
{
var requestBody = await new StreamReader(request.Body).ReadToEndAsync();
var cloudEvents = CloudEvent.ParseMany(BinaryData.FromString(requestBody), skipValidation: true).ToList();
dynamic document = null;
foreach (var cloudEvent in cloudEvents)
{
var parsedEvent = CallAutomationEventParser.Parse(cloudEvent);
_logger.LogInformation($"Received event of type {cloudEvent.Type} with call connection id {parsedEvent.CallConnectionId}");
var callConnection = _callAutomationClient.GetCallConnection(parsedEvent.CallConnectionId);
var callMedia = callConnection.GetCallMedia();
if (parsedEvent is CallConnected callConnected)
{
var playSource = new TextSource($"You are connected - Id: {callConnected.CallConnectionId}")
{
SourceLocale = "en-US",
CustomVoiceEndpointId = _configuration["CustomVoiceEndpointId"],
VoiceName = _configuration["NeuralVoiceName"]
};
await callMedia.PlayToAllAsync(playSource);
document = new
{
id = Guid.NewGuid().ToString(),
callConnection.CallConnectionId,
CallState = "Answered"
};
}
}
return new HandleEventResponseModel
{
Result = request.CreateResponse(HttpStatusCode.OK),
Document = document
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment