Skip to content

Commit

Permalink
chore: test
Browse files Browse the repository at this point in the history
  • Loading branch information
Keyrxng committed Sep 8, 2024
1 parent 839a701 commit b440d0a
Showing 1 changed file with 79 additions and 0 deletions.
79 changes: 79 additions & 0 deletions src/bot/mtproto-api/workrooms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,85 @@ export async function createChat(context: Context<"issues.labeled", SupportedEve
})
);

const channel = await mtProto.client.invoke(
new mtProto.api.channels.CreateChannel({
title: chatName,
broadcast: false,
about: payload.issue.body || `- ${labelName} - ${payload.issue.html_url}`,
})
);

if ("id" in channel) {
chatId = channel.id
} else {
throw new Error("Failed to create channel");
}

await context.adapters.supabase.chats.saveChat(chatId, "channel - " + payload.issue.title, payload.issue.node_id);

const contacts = await mtProto.client.invoke(
new mtProto.api.contacts.GetContacts({ hash: undefined }),
);

let user;

if ("users" in contacts) {
user = contacts.users.find((user) => user.id.toJSNumber() === config.botId);
if (!user) {
throw new Error("Bot not found in contacts");
}
} else {
throw new Error("Failed to fetch contacts");
}

let accessHash;

if ("accessHash" in user) {
accessHash = user.accessHash;
}

if (!accessHash) {
throw new Error("Failed to fetch access hash");
}

let channelAccessHash;
let channelIdBigInt;

const chats = await mtProto.client.invoke(
new mtProto.api.messages.GetChats({ id: undefined }),
);

if ("chats" in chats) {
const foundChannel = chats.chats.find((chat) => chat.id.toJSNumber() === channel.id)
if (!foundChannel) {
throw new Error("Channel not found");
}

if ("accessHash" in foundChannel) {
channelAccessHash = foundChannel.accessHash;
}

if ("id" in foundChannel) {
channelIdBigInt = foundChannel.id;
}
}

if (!channelAccessHash) {
throw new Error("Failed to fetch channel access hash");
}

if (!channelIdBigInt) {
throw new Error("Failed to fetch channel id");
}

await mtProto.client.invoke(
new mtProto.api.channels.InviteToChannel({
channel: new mtProto.api.InputChannel({ channelId: channelIdBigInt, accessHash: channelAccessHash }),
users: [new mtProto.api.InputUser({ userId: user.id, accessHash })],
})
);


if ("chats" in chat.updates) {
chatId = chat.updates.chats[0].id.toJSNumber();
chatIdBigInt = chat.updates.chats[0].id;
Expand Down

0 comments on commit b440d0a

Please sign in to comment.