Skip to content

Commit

Permalink
Chat creation WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
matushorvath committed Oct 1, 2023
1 parent 70f492b commit 9a510bf
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 12 deletions.
19 changes: 17 additions & 2 deletions src/chat.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
const { TelegramClient } = require('./client');
const { getTdLibSecrets } = require('./secrets');

const botUserId = 5071613978;

let cachedClient;
const getTelegramClient = async () => {
if (cachedClient) {
Expand All @@ -28,8 +30,21 @@ const getTelegramClient = async () => {
const createNewChat = async () => {
const client = await getTelegramClient();

const chat = await client.createSupergroup('MH TEST AoC 2001 Day 25', 'MH Test Description');
console.log(chat);
const { chatId, supergroupId } = await client.createSupergroup('MH TEST AoC 2001 Day 25', 'MH Test Description');
console.debug(`createNewChat: Created chat ${chatId} supergroup ${supergroupId}`);

// TODO mark the chat as created somehow, so we don't repeatedly try to create the same chat over and over

await client.addChatAdmin(botUserId, chatId);
console.debug(`createNewChat: Added bot ${botUserId} as admin to chat ${chatId}`);
};

const transferChatOwnership = async (_newOwnerUserId) => {
const client = await getTelegramClient();

const result = await client.canTransferChatOwnership();
console.debug(result);
};

exports.createNewChat = createNewChat;
exports.transferChatOwnership = transferChatOwnership;
29 changes: 25 additions & 4 deletions src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ class TelegramClient {
let onUpdate;
return new Promise(resolve => {
onUpdate = (update) => {
//console.debug(JSON.stringify(update, undefined, 2));
// console.debug(JSON.stringify(update, undefined, 2));
if (filter(update)) {
updates.push(update);
}
Expand Down Expand Up @@ -189,14 +189,12 @@ class TelegramClient {
}

async createSupergroup(title, description) {
const chat = await this.client.invoke({
const chat = await this.clientInvoke({
_: 'createNewSupergroupChat',
title,
description
});

console.log(chat);

if (chat?._ !== 'chat' || !chat?.id || chat?.type?._ !== 'chatTypeSupergroup' || !chat?.type?.supergroup_id) {
throw new Error(`Invalid response: ${JSON.stringify(chat)}`);
}
Expand Down Expand Up @@ -271,6 +269,29 @@ class TelegramClient {
}
}

async canTransferChatOwnership() {
const result = await this.clientInvoke({
_: 'canTransferOwnership'
});

return {
canTransfer: result?._ === 'canTransferOwnershipResultOk',
result: result?._
};
}

async transferChatOwnership(chatId, newOwnerUserId) {
const status = await this.clientInvoke({
_: 'transferChatOwnership',
chat_id: chatId,
user_id: newOwnerUserId
});

if (status?._ !== 'ok') {
throw new Error(`Invalid response: ${JSON.stringify(status)}`);
}
}

async setChatDescription(chatId, description) {
const status = await this.clientInvoke({
_: 'setChatDescription',
Expand Down
22 changes: 16 additions & 6 deletions src/message.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const { updateLeaderboards } = require('./leaderboards');
const { formatBoard } = require('./board');
const { enableLogs, disableLogs, getLogsStatus, logActivity } = require('./logs');
const { loadStartTimes } = require('./times');
const { createNewChat } = require('./chat');
const { createNewChat, transferChatOwnership } = require('./chat');

const { DynamoDB } = require('@aws-sdk/client-dynamodb');
const luxon = require('luxon');
Expand Down Expand Up @@ -44,8 +44,10 @@ const onMessage = async (message) => {
await onCommandUpdate(message.chat.id, message.from, params?.trim());
} else if (command === 'board') {
await onCommandBoard(message.chat.id, params?.trim());
} else if (command === 'tmp') {
await onCommandTmp(message.chat.id, params?.trim());
} else if (command === 'tmp1') {
await onCommandTmp1(message.chat.id, params?.trim());
} else if (command === 'tmp2') {
await onCommandTmp2(message.chat.id, params?.trim());
} else if (command === 'start' || command === 'help') {
await onCommandHelp(message.chat.id);
} else {
Expand Down Expand Up @@ -258,12 +260,20 @@ const onCommandBoard = async (chat, params) => {
};

// TODO remove
const onCommandTmp = async (_chat, _params) => {
console.log('onCommandTmp: start');
const onCommandTmp1 = async (_chat, _params) => {
console.log('onCommandTmp1: start');

await createNewChat();

console.log('onCommandTmp: done');
console.log('onCommandTmp1: done');
};

const onCommandTmp2 = async (_chat, _params) => {
console.log('onCommandTmp2: start');

await transferChatOwnership();

console.log('onCommandTmp2: done');
};

const onCommandStatus = async (chat, telegramUser) => {
Expand Down

0 comments on commit 9a510bf

Please sign in to comment.