I would like to get the total number of messages in a conversation using NodeJS client. I found there is a method for Conversation: https://sdk.twilio.com/js/conversations/releases/2.1.0/docs/classes/Conversation.html#getMessagesCount
However, that method is not available in node lib: https://www.twilio.com/docs/conversations/api/conversation-resource
I am fetching a conversation by:
const userConversation = await client.conversations
.services(conversation_service_sid)
.users(user_id)
.userConversations(conversation_sid)
.fetch();
const conversation = await client.conversations
.services(conversation_service_sid)
.conversations(conversation_sid)
.fetch();
Both UserConversationInstance
and ConversationInstance
do not have that method.
I am using "twilio": "^3.70.0",
The way I can get number of messages by get the whole list of message and get its size. It must not be the best way to do. Could you help me another way to get it?
let messages = await client.conversations
.services(conversation_service_sid)
.conversations(conversation_sid)
.messages.list();
let messagesCount = messages.length;
Thanks
UPDATE using @twilio/conversations
:
import twilio from "twilio";
import { Client } from '@twilio/conversations';
const AccessToken = twilio.jwt.AccessToken;
const ChatGrant = AccessToken.ChatGrant;
const chatGrant = new ChatGrant({
serviceSid: conversationServiceSID,
});
const token = new AccessToken(
accountSid,
twilioApiKey,
twilioApiSecret,
{identity: userId}
);
token.addGrant(chatGrant);
const newclient = new Client(token.toJwt());
let conversation = await newclient.getConversationBySid(conversationSID);
let messagesCount = await conversation.getMessagesCount();
I think you're looking the the main nodejs package from twilio. However, you linked the docs of @twilio/conversations
.
You're probably fine when you switch to the other library there.