> ## 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.

# Telegram集成

> 使用LangChain Python与Telegram文档加载器集成。

> [Telegram即时通讯](https://web.telegram.org/a/)是一项全球可用的免费增值、跨平台、加密、基于云的集中式即时通讯服务。该应用还提供可选的端到端加密聊天、视频通话、VoIP、文件共享及多项其他功能。

本笔记本介绍如何从`Telegram`加载数据到可被LangChain处理的格式。

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from langchain_community.document_loaders import (
    TelegramChatApiLoader,
    TelegramChatFileLoader,
)
```

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
loader = TelegramChatFileLoader("example_data/telegram.json")
```

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
loader.load()
```

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
[Document(page_content="Henry on 2020-01-01T00:00:02: It's 2020...\n\nHenry on 2020-01-01T00:00:04: Fireworks!\n\nGrace ðŸ§¤ ðŸ\x8d’ on 2020-01-01T00:00:05: You're a minute late!\n\n", metadata={'source': 'example_data/telegram.json'})]
```

`TelegramChatApiLoader`直接从Telegram的任何指定聊天加载数据。要导出数据，您需要对Telegram账户进行身份验证。

您可以从[my.telegram.org/auth?to=apps](https://my.telegram.org/auth?to=apps)获取API\_HASH和API\_ID。

chat\_entity—建议使用频道的[实体](https://docs.telethon.dev/en/stable/concepts/entities.html?highlight=Entity#what-is-an-entity)。

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
loader = TelegramChatApiLoader(
    chat_entity="<CHAT_URL>",  # 建议此处使用Entity
    api_hash="<API HASH >",
    api_id="<API_ID>",
    username="",  # 仅在缓存会话时需要。
)
```

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
loader.load()
```

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
```

***

<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/document_loaders/telegram.mdx)或[提交问题](https://github.com/langchain-ai/docs/issues/new/choose)。
  </Callout>
</div>
