get_direct_chat_by_contact_number
Retrieve a direct WhatsApp chat JID using a contact's phone number. Use this to start a chat when only the number is known, though reliability may vary.
Instructions
Get direct WhatsApp chat JID by contact phone number (less reliable, use get_chat_by_id if JID is known).
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| phone_number | Yes | The phone number of the contact (e.g., 1234567890) |
Implementation Reference
- src/tools/chats.ts:170-216 (handler)The handler function that executes the 'get_direct_chat_by_contact_number' tool logic. It constructs a JID from the phone number, calls getChatById, and validates it's a direct (non-group) chat.
server.tool( "get_direct_chat_by_contact_number", "Get direct WhatsApp chat JID by contact phone number (less reliable, use get_chat_by_id if JID is known).", { phone_number: z .string() .describe("The phone number of the contact (e.g., 1234567890)"), }, async ({ phone_number }): Promise<CallToolResult> => { try { // Construct potential JID const jid = `${phone_number}@s.whatsapp.net`; const chat = await whatsappService.getChatById(jid); if (!chat || chat.isGroup) { // Ensure it's a direct chat return { content: [ { type: "text", text: `Direct chat not found for number: ${phone_number}`, }, ], isError: true, }; } return { // Return only the JID or the full chat object? Let's return the object. content: [{ type: "text", text: JSON.stringify(chat, null, 2) }], }; } catch (error: any) { log.error( `Error in get_direct_chat_by_contact_number tool for number ${phone_number}:`, error, ); // Don't expose detailed errors, just indicate not found return { content: [ { type: "text", text: `Could not find direct chat for number: ${phone_number}`, }, ], isError: true, }; } }, ); - src/tools/chats.ts:173-177 (schema)Zod schema defining the input parameter: phone_number (string).
{ phone_number: z .string() .describe("The phone number of the contact (e.g., 1234567890)"), }, - src/tools/chats.ts:170-216 (registration)Tool registration via server.tool() call in the registerChatTools function.
server.tool( "get_direct_chat_by_contact_number", "Get direct WhatsApp chat JID by contact phone number (less reliable, use get_chat_by_id if JID is known).", { phone_number: z .string() .describe("The phone number of the contact (e.g., 1234567890)"), }, async ({ phone_number }): Promise<CallToolResult> => { try { // Construct potential JID const jid = `${phone_number}@s.whatsapp.net`; const chat = await whatsappService.getChatById(jid); if (!chat || chat.isGroup) { // Ensure it's a direct chat return { content: [ { type: "text", text: `Direct chat not found for number: ${phone_number}`, }, ], isError: true, }; } return { // Return only the JID or the full chat object? Let's return the object. content: [{ type: "text", text: JSON.stringify(chat, null, 2) }], }; } catch (error: any) { log.error( `Error in get_direct_chat_by_contact_number tool for number ${phone_number}:`, error, ); // Don't expose detailed errors, just indicate not found return { content: [ { type: "text", text: `Could not find direct chat for number: ${phone_number}`, }, ], isError: true, }; } }, );