> ## Documentation Index
> Fetch the complete documentation index at: https://cndoc-langchain.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Twilio 集成

> 使用 LangChain Python 与 Twilio 工具集成。

本笔记本介绍如何使用 [Twilio](https://www.twilio.com) API 封装器通过 SMS 或 [Twilio 消息渠道](https://www.twilio.com/docs/messaging/channels)发送消息。

Twilio 消息渠道便于与第三方消息应用集成，并允许您通过 WhatsApp Business Platform（正式版）、Facebook Messenger（公开测试版）和 Google Business Messages（私有测试版）发送消息。

## 设置

要使用此工具，您需要安装 Python Twilio 包 `twilio`

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
pip install -qU  twilio
```

您还需要设置一个 Twilio 帐户并获取您的凭据。您需要您的帐户字符串标识符 (SID) 和您的身份验证令牌。您还需要一个用于发送消息的号码。

您可以将这些作为命名参数 `account_sid`、`auth_token`、`from_number` 传递给 TwilioAPIWrapper，或者您可以设置环境变量 `TWILIO_ACCOUNT_SID`、`TWILIO_AUTH_TOKEN`、`TWILIO_FROM_NUMBER`。

## 发送 SMS

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from langchain_community.utilities.twilio import TwilioAPIWrapper
```

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
twilio = TwilioAPIWrapper(
    #     account_sid="foo",
    #     auth_token="bar",
    #     from_number="baz,"
)
```

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
twilio.run("hello world", "+16162904619")
```

## 发送 WhatsApp 消息

您需要将您的 WhatsApp Business 帐户与 Twilio 关联。您还需要确保用于发送消息的号码在 Twilio 上配置为 WhatsApp 启用的发件人，并在 WhatsApp 上注册。

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from langchain_community.utilities.twilio import TwilioAPIWrapper
```

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
twilio = TwilioAPIWrapper(
    #     account_sid="foo",
    #     auth_token="bar",
    #     from_number="whatsapp: baz,"
)
```

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
twilio.run("hello world", "whatsapp: +16162904619")
```

***

<div className="source-links">
  <Callout icon="terminal-2">
    [将这些文档连接](/use-these-docs)到 Claude、VSCode 等，通过 MCP 获取实时答案。
  </Callout>

  <Callout icon="edit">
    [在 GitHub 上编辑此页面](https://github.com/langchain-ai/docs/edit/main/src/oss/python/integrations/tools/twilio.mdx) 或 [提交问题](https://github.com/langchain-ai/docs/issues/new/choose)。
  </Callout>
</div>
