Skip to content

Commit d3f940f

Browse files
yoavfclaude
andauthored
feat(agents): add getTelegramConnectURL method (#154)
* feat(agents): add getTelegramConnectURL method Add getTelegramConnectURL(agentName) to the agents module, enabling app developers to generate Telegram connect URLs for their in-app agents. Mirrors the existing getWhatsAppConnectURL pattern. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix: add missing comment for consistency with WhatsApp method Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * test(agents): add unit tests for agents module Covers conversations CRUD, addMessage, and connect URL generation for both WhatsApp and Telegram channels. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 0115b59 commit d3f940f

3 files changed

Lines changed: 137 additions & 0 deletions

File tree

src/modules/agents.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,20 @@ export function createAgentsModule({
112112
}
113113
};
114114

115+
const getTelegramConnectURL = (agentName: string) => {
116+
const baseUrl = `${serverUrl}/api/apps/${appId}/agents/${encodeURIComponent(
117+
agentName
118+
)}/telegram`;
119+
const accessToken = token ?? getAccessToken();
120+
121+
if (accessToken) {
122+
return `${baseUrl}?token=${accessToken}`;
123+
} else {
124+
// No token - URL will redirect to login automatically
125+
return baseUrl;
126+
}
127+
};
128+
115129
return {
116130
getConversations,
117131
getConversation,
@@ -120,5 +134,6 @@ export function createAgentsModule({
120134
addMessage,
121135
subscribeToConversation,
122136
getWhatsAppConnectURL,
137+
getTelegramConnectURL,
123138
};
124139
}

src/modules/agents.types.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,4 +405,25 @@ export interface AgentsModule {
405405
* ```
406406
*/
407407
getWhatsAppConnectURL(agentName: AgentName): string;
408+
409+
/**
410+
* Gets Telegram connection URL for an agent.
411+
*
412+
* Generates a URL that users can use to connect with the agent through Telegram.
413+
* The URL includes authentication if a token is available. When the user opens
414+
* this URL, they are redirected to the agent's Telegram bot with an activation
415+
* code that securely links their account.
416+
*
417+
* @param agentName - The name of the agent.
418+
* @returns Telegram connection URL.
419+
*
420+
* @example
421+
* ```typescript
422+
* // Get Telegram connection URL
423+
* const telegramUrl = base44.agents.getTelegramConnectURL('support-agent');
424+
* console.log(`Connect through Telegram: ${telegramUrl}`);
425+
* // User can open this URL to start a Telegram conversation
426+
* ```
427+
*/
428+
getTelegramConnectURL(agentName: AgentName): string;
408429
}

tests/unit/agents.test.ts

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
import { describe, test, expect, beforeEach, afterEach } from "vitest";
2+
import nock from "nock";
3+
import { createClient } from "../../src/index.ts";
4+
5+
describe("Agents Module", () => {
6+
let base44: ReturnType<typeof createClient>;
7+
let scope: nock.Scope;
8+
const appId = "test-app-id";
9+
const serverUrl = "https://api.base44.com";
10+
11+
beforeEach(() => {
12+
base44 = createClient({ serverUrl, appId });
13+
scope = nock(serverUrl);
14+
nock.disableNetConnect();
15+
});
16+
17+
afterEach(() => {
18+
nock.cleanAll();
19+
nock.enableNetConnect();
20+
});
21+
22+
describe("getConversations", () => {
23+
test("should fetch all conversations", async () => {
24+
const mockConversations = [
25+
{ id: "conv-1", agent_name: "support", messages: [] },
26+
{ id: "conv-2", agent_name: "sales", messages: [] },
27+
];
28+
scope.get(`/api/apps/${appId}/agents/conversations`).reply(200, mockConversations);
29+
30+
const result = await base44.agents.getConversations();
31+
expect(result).toEqual(mockConversations);
32+
});
33+
});
34+
35+
describe("getConversation", () => {
36+
test("should fetch a specific conversation", async () => {
37+
const mockConversation = { id: "conv-1", agent_name: "support", messages: [] };
38+
scope.get(`/api/apps/${appId}/agents/conversations/conv-1`).reply(200, mockConversation);
39+
40+
const result = await base44.agents.getConversation("conv-1");
41+
expect(result).toEqual(mockConversation);
42+
});
43+
});
44+
45+
describe("createConversation", () => {
46+
test("should create a conversation", async () => {
47+
const created = { id: "conv-new", agent_name: "support", messages: [] };
48+
scope.post(`/api/apps/${appId}/agents/conversations`).reply(200, created);
49+
50+
const result = await base44.agents.createConversation({ agent_name: "support" });
51+
expect(result).toEqual(created);
52+
});
53+
});
54+
55+
describe("addMessage", () => {
56+
test("should post to v2 endpoint", async () => {
57+
const conversation = { id: "conv-1", agent_name: "support", messages: [] } as any;
58+
const response = { id: "msg-1", role: "assistant", content: "Hello!" };
59+
scope.post(`/api/apps/${appId}/agents/conversations/v2/conv-1/messages`).reply(200, response);
60+
61+
const result = await base44.agents.addMessage(conversation, { role: "user", content: "Hi" });
62+
expect(result).toEqual(response);
63+
});
64+
});
65+
66+
describe("getWhatsAppConnectURL", () => {
67+
test("should return URL without token when no auth", () => {
68+
const url = base44.agents.getWhatsAppConnectURL("support");
69+
expect(url).toBe(`${serverUrl}/api/apps/${appId}/agents/support/whatsapp`);
70+
});
71+
72+
test("should include token when authenticated", () => {
73+
const authed = createClient({ serverUrl, appId, token: "test-token" });
74+
const url = authed.agents.getWhatsAppConnectURL("support");
75+
expect(url).toBe(`${serverUrl}/api/apps/${appId}/agents/support/whatsapp?token=test-token`);
76+
});
77+
78+
test("should encode agent name", () => {
79+
const url = base44.agents.getWhatsAppConnectURL("my agent");
80+
expect(url).toBe(`${serverUrl}/api/apps/${appId}/agents/my%20agent/whatsapp`);
81+
});
82+
});
83+
84+
describe("getTelegramConnectURL", () => {
85+
test("should return URL without token when no auth", () => {
86+
const url = base44.agents.getTelegramConnectURL("support");
87+
expect(url).toBe(`${serverUrl}/api/apps/${appId}/agents/support/telegram`);
88+
});
89+
90+
test("should include token when authenticated", () => {
91+
const authed = createClient({ serverUrl, appId, token: "test-token" });
92+
const url = authed.agents.getTelegramConnectURL("support");
93+
expect(url).toBe(`${serverUrl}/api/apps/${appId}/agents/support/telegram?token=test-token`);
94+
});
95+
96+
test("should encode agent name", () => {
97+
const url = base44.agents.getTelegramConnectURL("my agent");
98+
expect(url).toBe(`${serverUrl}/api/apps/${appId}/agents/my%20agent/telegram`);
99+
});
100+
});
101+
});

0 commit comments

Comments
 (0)