How to Overwrite Chat Messages #3438
jpeng-ms
started this conversation in
Show and tell
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
If you are using
CallwithChat
Composite and you would like to access messages sending out and modify it with some custom logic, then this tutorial is right for you!In this tutorial we will be creating a custom logic first, embed it as options when creating chat client, then create call with chat adapter as how you normally do.
First of all, we need to create a new
policy
and you can think of it as a vessel that contains our custom logic:Noticing how a policy consists of two parts - name and a function call back. The callback function that you provided would be triggered for each request going out. And here is where we need to provide our logic to.
Since we want to eliminate noises and only want to check requests that send messages, we need the
if (request.method === 'POST' && request.url.includes('/messages'))
to locatesendMessage
requests sent from the Chat SDK (@azure/communication-chat
) and the endpoint that handlessendMessage
requests is<endpointURL>/chat/thread/<threadID>/messages
Then, we will have access to the
requst.body
which contains the chat message that's about to be sent out.Next, we need to wrap the policy as part of
chatClientOptions
:Noticing we have a new property named
position
and we need to set it toperCall
so our logic (aka. "policy") would be trigger for each call.In order to let Chat client know we have these options, we need to create chat client like this:
Noticing the
statefulChatClientOptions
is the same object we just created that contains ourpolicy
.Now we can go ahead to create our adapter:
Noticing the
chatClient
that we provided here is the samechatClient
we just created with ourpolicy
.That's it! Now you know how you can overwrite message content, feel free to give a try and let us know if you have any issues.
Beta Was this translation helpful? Give feedback.
All reactions