LangChain Infobip Integration - Sending SMS and Emails Programmatically

Posted: Nov 9, 2024.

The InfobipAPIWrapper in LangChain provides a convenient way to integrate Infobip's messaging services into your applications. With this wrapper, you can programmatically send SMS messages and emails through Infobip's API infrastructure.

What is InfobipAPIWrapper?

InfobipAPIWrapper is a utility class that wraps around Infobip's API services, allowing you to send messages across different channels like SMS and email. It simplifies the process of integrating Infobip's messaging capabilities into your LangChain applications by providing a straightforward interface to their API.

Reference

MethodDescription
run(body, to, sender, subject, channel)Sends a message through the specified channel. Parameters vary based on the channel type (SMS or email).

Configuration parameters:

  • infobip_api_key: Your Infobip API key for authentication
  • infobip_base_url: The base URL for Infobip API (defaults to 'https://api.infobip.com')

How to Use InfobipAPIWrapper

Initial Setup

Before using the wrapper, you need to set up your Infobip credentials. You can either pass them directly to the wrapper or set them as environment variables:

from langchain_community.utilities.infobip import InfobipAPIWrapper

# Option 1: Direct initialization with credentials
infobip = InfobipAPIWrapper(
    infobip_api_key="your-api-key",
    infobip_base_url="https://api.infobip.com"
)

# Option 2: Using environment variables
# Set INFOBIP_API_KEY and INFOBIP_BASE_URL environment variables
infobip = InfobipAPIWrapper()

Sending SMS Messages

To send an SMS message, use the run method with the "sms" channel:

infobip.run(
    to="41793026727",  # Recipient's phone number
    body="Hello, This is a test SMS!",
    sender="Langchain",  # Sender ID
    channel="sms"
)

Sending Emails

For sending emails, use the "email" channel and include the subject:

infobip.run(
    to="recipient@example.com",
    body="This is the email content",
    sender="your-email@example.com",
    subject="Test Email Subject",
    channel="email"
)

Integration with LangChain Agents

You can also integrate the InfobipAPIWrapper into LangChain agents for more complex workflows. Here's an example using a structured tool:

from langchain_core.tools import StructuredTool
from pydantic import BaseModel, Field

# Define the input schema for email
class EmailInput(BaseModel):
    body: str = Field(description="Email body text")
    to: str = Field(description="Email address to send to")
    sender: str = Field(description="Email address to send from")
    subject: str = Field(description="Email subject")
    channel: str = Field(description="Must be 'email'")

# Create the wrapper and tool
infobip_wrapper = InfobipAPIWrapper()
email_tool = StructuredTool.from_function(
    name="send_email",
    description="Send an email via Infobip",
    func=infobip_wrapper.run,
    args_schema=EmailInput
)

# Use the tool in your agent
tools = [email_tool]
# ... rest of your agent setup

This implementation allows you to use Infobip's messaging capabilities as part of your LangChain agent's toolset, enabling automated sending of messages based on agent decisions or user interactions.

Remember to handle your API credentials securely and follow Infobip's best practices for sending messages. The wrapper provides a simple interface, but you should still consider rate limits, delivery reports, and other Infobip-specific features when building your application.

Error Handling

When using the InfobipAPIWrapper, it's recommended to implement proper error handling:

try:
    response = infobip.run(
        to="recipient@example.com",
        body="Test message",
        sender="sender@example.com",
        subject="Test",
        channel="email"
    )
    print(f"Message sent successfully: {response}")
except Exception as e:
    print(f"Error sending message: {str(e)}")

This will help you catch and handle any API errors or connectivity issues that might occur during message sending.

An alternative to LangSmith

Open-source LangChain monitoring, prompt management, and magic. Get started in 2 minutes.

LangChain Docs

Join 10,000+ subscribers

Every 2 weeks, latest model releases and industry news.

An alternative to LangSmith

Open-source LangChain monitoring, prompt management, and magic. Get started in 2 minutes.

LangChain Docs