如何用 TwexAPI Ask Grok
在自动化流程里,Grok 最有价值的用法不是随手问一个问题,而是基于你已经采集到的证据做分析:搜索到的推文、DM 会话摘要、账号状态检查,或活动复盘数据。真正需要保存的不只是答案,还包括你问了什么、用了哪些来源数据、哪个 X 账号发起请求,以及下一次是否要继续同一个 Grok 对话。
TwexAPI 的 Ask Grok 端点通过 POST /v3/twitter/grok/ask,使用 Twitter/X 账号 cookie 向 Grok 发送 prompt。
这个端点返回 Grok 的回答。它不会帮你抓推文、读取 DM 历史或自动构造 prompt。正确流程是先拉取 X 数据,再用已保存的上下文构造清晰 prompt,然后从服务端调用 Ask Grok。
端点和请求字段
使用 POST /v3/twitter/grok/ask 从服务端流程里向 Grok 提问。
| 字段 | 类型 | 必填 | 说明 |
|---|---|---|---|
prompt | string | 是 | 发送给 Grok 的用户 prompt。建议包含来源上下文和任务。 |
cookie | string | 是 | X 认证 cookie 或 auth_token。只保存在服务端。 |
conversation_id | string 或 null | 否 | 继续已有 Grok conversation 时传入。 |
model | string 或 null | 否 | 可选 model ID。API 默认值是 grok-3-latest。 |
proxy | string 或 null | 否 | 可选 HTTP proxy。 |
这里的 conversation_id 属于 Grok。不要和 DM 端点里的 XChat conversation_id 混在一起。
基础请求
curl --request POST \
--url https://api.twexapi.io/v3/twitter/grok/ask \
--header "Authorization: Bearer $TWEXAPI_BEARER_TOKEN" \
--header "Content-Type: application/json" \
--data '{
"prompt": "把下面五条 X 帖子总结成三个风险和三个后续动作,回答保持简洁。Source posts: ...",
"model": "grok-3-latest",
"cookie": "auth_token=...; ct0=...; twid=..."
}'响应会返回 Grok conversation ID 和最终回答。
1{
2 "code": 200,
3 "msg": "success",
4 "data": {
5 "conversation_id": "2076320454898278729",
6 "user_chat_item_id": "2076320454898278730",
7 "agent_chat_item_id": "2076320454898278731",
8 "message": "主要风险是...",
9 "reasoning": "Thinking about your request",
10 "soft_stop": false,
11 "follow_up_suggestions": [
12 {
13 "label": "Turn this into an action plan",
14 "properties": null,
15 "tools_overrides": null
16 }
17 ]
18 }
19}如果下一次 prompt 要继续同一个 Grok 对话,就保存返回的 conversation_id。把 message 当作回答保存,同时保留原始响应用于审计。
用已保存的 X 数据构造 Prompt
Prompt 要窄一点。一个可复核的 prompt 通常包含任务、来源记录和输出格式。
1Task:
2Summarize the following X posts into customer objections and sales follow-up actions.
3
4Source records:
5- tweet_id=...
6 author=...
7 created_at=...
8 text=...
9 metrics=...
10
11Output:
121. Top objections
132. Evidence from source posts
143. Follow-up actions
154. Questions that need human review不要让 Grok 猜测你的来源数据里没有的内容。如果来源记录不完整,就在 prompt 里直接说明。
Python Grok Client
这个脚本读取已保存的来源记录,构造 prompt,调用 Grok,并保存足够的 metadata,方便之后复盘。
1import os
2from datetime import datetime, timezone
3from typing import Any
4
5import requests
6
7API_URL = "https://api.twexapi.io/v3/twitter/grok/ask"
8BEARER_TOKEN = os.environ["TWEXAPI_BEARER_TOKEN"]
9X_COOKIE = os.environ["TWEXAPI_X_COOKIE"]
10GROK_MODEL = os.environ.get("TWEXAPI_GROK_MODEL", "grok-3-latest")
11PROXY = os.environ.get("TWEXAPI_PROXY")
12
13def build_prompt(source_records: list[dict[str, Any]]) -> str:
14 lines = [
15 "Task:",
16 "Summarize these X posts into customer objections and follow-up actions.",
17 "",
18 "Source records:",
19 ]
20 for item in source_records:
21 lines.extend(
22 [
23 f"- tweet_id={item['tweet_id']}",
24 f" author={item.get('author', 'unknown')}",
25 f" created_at={item.get('created_at', 'unknown')}",
26 f" text={item.get('text', '')}",
27 f" metrics={item.get('metrics', {})}",
28 ]
29 )
30 lines.extend(
31 [
32 "",
33 "Output:",
34 "1. Top objections",
35 "2. Evidence from source posts",
36 "3. Follow-up actions",
37 "4. Questions that need human review",
38 ]
39 )
40 return "\n".join(lines)
41
42def ask_grok(
43 prompt: str,
44 *,
45 conversation_id: str | None = None,
46) -> dict[str, Any]:
47 payload: dict[str, Any] = {
48 "prompt": prompt,
49 "cookie": X_COOKIE,
50 "model": GROK_MODEL,
51 }
52 if conversation_id:
53 payload["conversation_id"] = conversation_id
54 if PROXY:
55 payload["proxy"] = PROXY
56
57 response = requests.post(
58 API_URL,
59 headers={
60 "Authorization": f"Bearer {BEARER_TOKEN}",
61 "Content-Type": "application/json",
62 "Accept": "application/json",
63 },
64 json=payload,
65 timeout=120,
66 )
67 response.raise_for_status()
68 data = response.json()
69
70 if data.get("code") != 200:
71 raise RuntimeError(data.get("msg", "TwexAPI returned an error"))
72
73 return data["data"]
74
75def normalize_grok_result(
76 *,
77 source_job_id: str,
78 prompt: str,
79 result: dict[str, Any],
80) -> dict[str, Any]:
81 return {
82 "source_job_id": source_job_id,
83 "prompt": prompt,
84 "grok_conversation_id": result["conversation_id"],
85 "user_chat_item_id": result.get("user_chat_item_id"),
86 "agent_chat_item_id": result.get("agent_chat_item_id"),
87 "message": result["message"],
88 "reasoning": result.get("reasoning"),
89 "soft_stop": result.get("soft_stop"),
90 "follow_up_suggestions": result.get("follow_up_suggestions") or [],
91 "model": GROK_MODEL,
92 "asked_at": datetime.now(timezone.utc).isoformat(),
93 "raw": result,
94 }
95
96if __name__ == "__main__":
97 source_records = [
98 {
99 "tweet_id": "1234567890123456789",
100 "author": "example_user",
101 "created_at": "2026-07-16T09:00:00Z",
102 "text": "导出功能正常,但 cursor 过期时需要更清晰的错误提示。",
103 "metrics": {"likes": 12, "replies": 3},
104 }
105 ]
106 prompt = build_prompt(source_records)
107 result = ask_grok(prompt)
108 print(
109 normalize_grok_result(
110 source_job_id="weekly-objection-review-2026-07-16",
111 prompt=prompt,
112 result=result,
113 )
114 )运行脚本前,先在服务端环境设置 TWEXAPI_BEARER_TOKEN 和 TWEXAPI_X_COOKIE。不要在浏览器里调用这个端点,因为请求需要 X cookie。
继续同一个 Grok 对话
响应里返回的 conversation_id 应保存为 grok_conversation_id。只有当下一次请求确实要继续同一个 Grok thread 时,才把它传回去。
follow_up = ask_grok(
"Turn the previous answer into a support team checklist.",
conversation_id=saved_grok_conversation_id,
)命名要明确。conversation_id 这个字段在 XChat 工作流里也会出现,所以数据库里建议存成 grok_conversation_id。
Grok 分析工作流
- 采集来源数据:用对应 TwexAPI 端点搜索帖子、导出 DM 历史或拉取账号数据。
- 保存原始来源:保留 tweet ID、时间、作者、指标、DM message ID 等证据。
- 构造 prompt:只放当前任务需要的记录,并说明输出格式。
- Ask Grok:调用
POST /v3/twitter/grok/ask,传prompt、cookie和可选model。 - 保存结果:保存
message、conversation_id、chat item IDs、soft_stop、follow-up suggestions、prompt 和原始响应。 - 行动前复核:把 Grok 输出当作分析。外发消息、公开发布或客户决策仍应经过人工审批。
这样答案更容易被信任,因为每次回答都能追溯到 prompt 里使用的来源记录。
应该保存哪些字段
| 字段 | 作用 |
|---|---|
source_job_id | 把 Grok 回答关联到数据采集任务。 |
prompt | 说明当时到底问了什么。 |
grok_conversation_id | 后续请求继续同一个 Grok thread。 |
user_chat_item_id | 可用时关联用户 prompt item。 |
agent_chat_item_id | 可用时关联 Grok response item。 |
message | 最终回答。 |
reasoning | API 返回的可选 reasoning text。 |
soft_stop | 说明回答是否 soft-stopped。 |
follow_up_suggestions | Grok 建议的后续 prompt。 |
model | 记录请求使用的 model。 |
asked_at | 审计和复盘时间。 |
| 原始 response JSON | 字段映射变化时可重新处理。 |
不要把完整账号 cookie 存在 Grok 结果里。保存凭据引用即可。
常见错误
- 把 Grok
conversation_id和 XChatconversation_id混在一起。 - prompt 很宽泛、没有来源记录,却把回答当成证据。
- 从客户端代码调用端点,暴露 X cookie。
- 没有按 job 或账号隔离保存的 Grok conversation ID,导致续错对话。
- 让 Grok 输出直接触发公开动作,而没有人工复核。
总结
当你希望 Grok 分析或总结已经采集好的 X 数据时,使用 POST /v3/twitter/grok/ask。发送清晰的 prompt,把 X cookie 留在服务端,保存返回的 Grok conversation_id,并把回答和支撑它的来源记录绑在一起。