Skip to content

Commit

Permalink
Chat creation WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
matushorvath committed Apr 25, 2023
1 parent 485f35e commit 08a935d
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 28 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;
49 changes: 30 additions & 19 deletions src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,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 @@ -170,8 +170,6 @@ class TelegramClient {
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 All @@ -183,24 +181,16 @@ class TelegramClient {
}

async addChatAdmin(userId, chatId) {
const added = await this.client.invoke({
const statusAdd = await this.client.invoke({
_: 'addChatMember',
chat_id: chatId,
user_id: userId
});

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

const messageFromBotFilter = (update) => {
return update?._ === 'updateNewMessage'
&& update?.message?.sender_id?._ === 'messageSenderUser'
&& update?.message?.sender_id?.user_id === userId
&& update?.message?.chat_id === chatId;
};
const updatesPromise = this.waitForUpdates(messageFromBotFilter);

const statusSet = await this.client.invoke({
_: 'setChatMemberStatus',
chat_id: chatId,
Expand All @@ -227,12 +217,10 @@ class TelegramClient {
if (statusSet?._ !== 'ok') {
throw new Error(`Invalid response: ${JSON.stringify(statusSet)}`);
}

return (await updatesPromise)[0]?.message?.content?.text?.text;
}

async removeChatMember(userId, chatId) {
const statusSet = await this.client.invoke({
const status = await this.client.invoke({
_: 'setChatMemberStatus',
chat_id: chatId,
member_id: {
Expand All @@ -244,8 +232,31 @@ class TelegramClient {
}
});

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

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

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

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

if (status?._ !== 'ok') {
throw new Error(`Invalid response: ${JSON.stringify(status)}`);
}
}
}
Expand Down
24 changes: 17 additions & 7 deletions src/telegram.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const { formatBoard } = require('./board');
const { addYear } = require('./years');
const { enableLogs, disableLogs, 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 @@ -72,7 +72,7 @@ const onMessage = async (message) => {
return;
}

let m = message.text.match(/^\s*\/([a-z]+)(?:\s+(.+))?\s*$/);
let m = message.text.match(/^\s*\/([a-z0-9]+)(?:\s+(.+))?\s*$/);
if (!m) {
console.log(`onMessage: text '${message.text}' did not match`);
await onCommandUnknown(message.chat.id, message.text);
Expand All @@ -94,8 +94,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 @@ -294,12 +296,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 08a935d

Please sign in to comment.