Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Telegram direct link to group #1687

Merged
merged 2 commits into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -185,18 +185,20 @@ export async function fetchUserTelegramChats(

// Fetch a list of Telegram chats that can be joined with the status of user
// The list is sorted such that chat a user hasn't joined are returned first
// If a chatId is provided, only chats with that id are returned.
export async function fetchTelegramChatsWithMembershipStatus(
client: Pool,
userId: number
userId: number,
chatId?: number
): Promise<ChatIDWithEventsAndMembership[]> {
const result = await sqlQuery(
client,
`
SELECT
tbe.telegram_chat_id AS "telegramChatID",
ARRAY_AGG(dpei.event_name) AS "eventNames",
ARRAY_AGG(tbe.ticket_event_id) AS "ticketEventIds",
CASE WHEN tbc.telegram_user_id IS NOT NULL THEN true ELSE false END AS "isChatMember"
ARRAY_AGG(DISTINCT dpei.event_name) AS "eventNames",
ARRAY_AGG(DISTINCT tbe.ticket_event_id) AS "ticketEventIds",
BOOL_OR(tbc.telegram_user_id IS NOT NULL) AS "isChatMember"
FROM
telegram_bot_events tbe
LEFT JOIN
Expand All @@ -207,12 +209,14 @@ export async function fetchTelegramChatsWithMembershipStatus(
devconnect_pretix_events_info dpei
ON
tbe.ticket_event_id = dpei.pretix_events_config_id
WHERE
($2::bigint IS NULL OR tbe.telegram_chat_id = $2::bigint)
GROUP BY
tbe.telegram_chat_id, tbc.telegram_user_id
tbe.telegram_chat_id
ORDER BY
"isChatMember" ASC;
`,
[userId]
[userId, chatId]
);
return result.rows;
}
Expand Down
26 changes: 24 additions & 2 deletions apps/passport-server/src/services/telegramService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ import {
eventsToLink,
generateReactProofUrl,
getBotURL,
getChatsWithMembershipStatus,
getDisplayEmojis,
getGroupChat,
getSessionKey,
Expand Down Expand Up @@ -288,8 +289,28 @@ export class TelegramService {
if (username) span?.setAttribute("username", username);
const firstName = ctx?.from?.first_name;
const name = firstName || username;
ctx.session.directLinkMode = false;
if (ctx.match && Number.isInteger(Number(ctx.match))) {
const [chatWithMembership] = await getChatsWithMembershipStatus(
ctx.session.dbPool,
ctx,
userId,
Number(ctx.match)
);
if (chatWithMembership) {
ctx.session.chatToJoin = chatWithMembership;
ctx.session.directLinkMode = true;
const chatTitle = chatWithMembership.chat?.title;
return await ctx.reply(
`Welcome ${name}! 👋\n\nClick the button below to join${
chatTitle ? ` <b>${chatTitle}</b>` : ""
}.\n\nYou will sign in to Zupass, then ZK prove you have a valid ticket.`,
{ reply_markup: zupassMenu, parse_mode: "HTML" }
);
}
}
await ctx.reply(
`Welcome ${name}! 👋\n\nClick the group you want to join.\n\nYou will sign in to Zupass, then ZK prove you have a ticket for one of the group's events.\n\nSee you soon 😽`,
`Welcome ${name}! 👋\n\nClick the group you want to join.\n\nYou will sign in to Zupass, then ZK prove you have a ticket for one of the group's events.`,
{ reply_markup: zupassMenu }
);
}
Expand Down Expand Up @@ -1708,7 +1729,8 @@ export async function startTelegramService(
dbPool: context.dbPool,
anonBotExists,
authBotURL,
anonBotURL
anonBotURL,
directLinkMode: false
});

if (anonBotExists) {
Expand Down
19 changes: 12 additions & 7 deletions apps/passport-server/src/util/telegramHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ export interface SessionData {
selectedEvent?: LinkedPretixTelegramEvent;
anonBotExists: boolean;
authBotURL: string;
directLinkMode: boolean;
anonBotURL: string;
lastMessageId?: number;
selectedChat?: TopicChat;
Expand Down Expand Up @@ -559,17 +560,19 @@ export const generateReactProofUrl = async (
});
};

const getChatsWithMembershipStatus = async (
export const getChatsWithMembershipStatus = async (
db: Pool,
ctx: BotContext,
userId: number
userId: number,
chatId?: number // if chatId is provided, only fetch chats with that id
): Promise<ChatIDWithChat<ChatIDWithEventsAndMembership>[]> => {
return traced("telegram", "getChatsWithMembershipStatus", async (span) => {
span?.setAttribute("userId", userId.toString());

const chatIdsWithMembership = await fetchTelegramChatsWithMembershipStatus(
db,
userId
userId,
chatId
);
const chatsWithMembership = await chatIDsToChats(
ctx,
Expand Down Expand Up @@ -741,10 +744,12 @@ export const chatsToJoinV2 = async (
);

range.webApp(`Join ${chat.chat?.title}`, proofUrl).row();
range.text(`↰ Back`, async (ctx) => {
ctx.session.chatToJoin = undefined;
ctx.menu.update();
});
if (!ctx.session.directLinkMode) {
range.text(`↰ Back`, async (ctx) => {
ctx.session.chatToJoin = undefined;
ctx.menu.update();
});
}
} else {
const chatsWithMembership = await getChatsWithMembershipStatus(
db,
Expand Down
Loading