Skip to content

Intercom

IntercomReader #

Bases: BaseReader

Intercom 读取器。从 Intercom 工作空间读取数据。

Parameters:

Name Type Description Default
personal_access_token str

Intercom 令牌。

required
Source code in llama_index/readers/intercom/base.py
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
class IntercomReader(BaseReader):
    """Intercom 读取器。从 Intercom 工作空间读取数据。

    Args:
        personal_access_token (str): Intercom 令牌。"""

    def __init__(self, intercom_access_token: str) -> None:
        """初始化Intercom读取器。"""
        self.intercom_access_token = intercom_access_token

    def load_data(self) -> List[Document]:
        """从工作区加载数据。

Args:
    workspace_id (str): 工作区ID。

Returns:
    List[Document]: 文档列表。
"""
        from bs4 import BeautifulSoup

        results = []

        articles = self.get_all_articles()

        for article in articles:
            body = article["body"]
            soup = BeautifulSoup(body, "html.parser")
            body = soup.get_text()

            extra_info = {
                "id": article["id"],
                "title": article["title"],
                "url": article["url"],
                "updated_at": article["updated_at"],
            }

            results.append(
                Document(
                    text=body,
                    extra_info=extra_info or {},
                )
            )

        return results

    def get_all_articles(self):
        articles = []
        next_page = None

        while True:
            response = self.get_articles_page(next_page)
            articles.extend(response["articles"])
            next_page = response["next_page"]

            if next_page is None:
                break

        return articles

    def get_articles_page(self, next_page: str = None):
        import requests

        if next_page is None:
            url = "https://api.intercom.io/articles"
        else:
            url = next_page

        headers = {
            "accept": "application/json",
            "Intercom-Version": "2.8",
            "authorization": f"Bearer {self.intercom_access_token}",
        }

        response = requests.get(url, headers=headers)

        response_json = json.loads(response.text)

        next_page = response_json.get("pages", {}).get("next", None)

        articles = response_json.get("data", [])

        return {"articles": articles, "next_page": next_page}

load_data #

load_data() -> List[Document]

从工作区加载数据。

Parameters:

Name Type Description Default
workspace_id str

工作区ID。

required

Returns:

Type Description
List[Document]

List[Document]: 文档列表。

Source code in llama_index/readers/intercom/base.py
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
    def load_data(self) -> List[Document]:
        """从工作区加载数据。

Args:
    workspace_id (str): 工作区ID。

Returns:
    List[Document]: 文档列表。
"""
        from bs4 import BeautifulSoup

        results = []

        articles = self.get_all_articles()

        for article in articles:
            body = article["body"]
            soup = BeautifulSoup(body, "html.parser")
            body = soup.get_text()

            extra_info = {
                "id": article["id"],
                "title": article["title"],
                "url": article["url"],
                "updated_at": article["updated_at"],
            }

            results.append(
                Document(
                    text=body,
                    extra_info=extra_info or {},
                )
            )

        return results