In this example, we first calculate the call duration in seconds using the current time and the call start time stored in the state object.
We then call the updateEngagement method with the engagement ID and the metadata object containing the call_duration field set to the calculated duration in seconds.
Finally, we call the callEnded method to mark the call as ended.
Note that the engagement must have been created with createEngagement: true for the updateEngagement method to work. If createEngagement is set to false, you will need to create the engagement first and then update it with the call duration.