Skip to main content
Glama
Jem-HR
by Jem-HR

get_templates

Retrieve available WhatsApp message templates to use for business communication, with options to filter by name or limit results.

Instructions

Get list of available WhatsApp templates.

Args: limit: Maximum number of templates to return (default: 100) name: Optional template name filter

Returns: Dictionary with templates list

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
limitNo
nameNo

Implementation Reference

  • The main handler implementation of get_templates tool that retrieves WhatsApp templates from the PyWA client, converts them to a simple dictionary format, and returns them with success status and count
    async def get_templates(
        limit: int = 100,
        *,
        name: Optional[str] = None,
    ) -> dict:
        """
        Get list of available WhatsApp templates.
        
        Args:
            limit: Maximum number of templates to return (default: 100)
            name: Optional template name filter
        
        Returns:
            Dictionary with templates list
        """
        try:
            # Note: PyWA's get_templates method signature may vary
            # This is a simplified implementation
            templates = wa_client.get_templates(
                limit=limit,
                name=name,
            )
            
            # Convert templates to simple format
            template_list = []
            for template in templates:
                template_data = {
                    "id": getattr(template, 'id', ''),
                    "name": getattr(template, 'name', ''),
                    "language": getattr(template, 'language', ''),
                    "status": getattr(template, 'status', ''),
                    "category": getattr(template, 'category', ''),
                }
                template_list.append(template_data)
            
            logger.info(f"Retrieved {len(template_list)} templates")
            return {
                "success": True,
                "templates": template_list,
                "count": len(template_list)
            }
        except Exception as e:
            logger.error(f"Failed to get templates: {str(e)}")
            return {"success": False, "error": str(e)}
  • The register_template_tools function that contains both send_template and get_templates tools, decorated with @mcp.tool() for MCP registration
    def register_template_tools(mcp, wa_client: WhatsApp):
        """Register template-related tools."""
        
        @mcp.tool()
        async def send_template(
            to: str,
            name: str,
            language: str = "en",
            params: Optional[List[Dict[str, Any]]] = None,
            *,
            reply_to_message_id: Optional[str] = None,
        ) -> dict:
            """
            Send a WhatsApp template message.
            
            Args:
                to: Phone number or WhatsApp ID
                name: Template name/ID
                language: Template language code (default: en)
                params: Optional template parameters (header, body, button components)
                reply_to_message_id: Message ID to reply to
            
            Returns:
                Dictionary with success status and message ID
            """
            try:
                result = wa_client.send_template(
                    to=to,
                    name=name,
                    language=language,
                    params=params or [],
                    reply_to_message_id=reply_to_message_id,
                )
                
                logger.info(f"Template '{name}' sent to {to}")
                message_id = getattr(result, 'id', str(result)) if result else None
                return {
                    "success": True,
                    "message_id": message_id,
                    "template": name,
                    "to": to,
                }
            except Exception as e:
                logger.error(f"Failed to send template: {str(e)}")
                return {"success": False, "error": str(e)}
        
        
        @mcp.tool()
        async def get_templates(
            limit: int = 100,
            *,
            name: Optional[str] = None,
        ) -> dict:
            """
            Get list of available WhatsApp templates.
            
            Args:
                limit: Maximum number of templates to return (default: 100)
                name: Optional template name filter
            
            Returns:
                Dictionary with templates list
            """
            try:
                # Note: PyWA's get_templates method signature may vary
                # This is a simplified implementation
                templates = wa_client.get_templates(
                    limit=limit,
                    name=name,
                )
                
                # Convert templates to simple format
                template_list = []
                for template in templates:
                    template_data = {
                        "id": getattr(template, 'id', ''),
                        "name": getattr(template, 'name', ''),
                        "language": getattr(template, 'language', ''),
                        "status": getattr(template, 'status', ''),
                        "category": getattr(template, 'category', ''),
                    }
                    template_list.append(template_data)
                
                logger.info(f"Retrieved {len(template_list)} templates")
                return {
                    "success": True,
                    "templates": template_list,
                    "count": len(template_list)
                }
            except Exception as e:
                logger.error(f"Failed to get templates: {str(e)}")
                return {"success": False, "error": str(e)}
  • The register_all_tools function that calls register_template_tools to register the get_templates tool with the MCP server
    def register_all_tools(mcp, wa_client):
        """Register all available tools with the MCP server."""
        register_messaging_tools(mcp, wa_client)
        register_interactive_tools(mcp, wa_client)
        register_template_tools(mcp, wa_client)

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/Jem-HR/pywa-mcp-server'

If you have feedback or need assistance with the MCP directory API, please join our Discord server