Skip to content

Commit

Permalink
update readme and example config
Browse files Browse the repository at this point in the history
  • Loading branch information
TheodoreKrypton committed Jan 27, 2024
1 parent d2fa77e commit 8320cac
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 32 deletions.
28 changes: 22 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,32 +94,48 @@ A config file will be auto-generated when you run the program for the first time

1. Duplicate the `example-config.yaml` file and name it `config.yaml`

#### Set up account details
#### Set up account details ([why do I need this?](#FAQ))

1. Go to [Here](https://my.telegram.org/apps), login with your phone number and create a Telegram app.
2. Copy the `api_id` and `api_hash` from the Telegram app page (step 2) to the config file (`telegram -> api_id / api_hash`)
2. Copy the `api_id` and `api_hash` from the Telegram app page (step 2) to the config file (`telegram -> account -> api_id / api_hash`)

#### Set up the channel to store files

1. Create a new Telegram private channel (New Channel in the menu on the left)
2. There should be a message like "Channel created". Right click the message and copy the post link.
3. The format of the link should be like `https://t.me/c/1234567/1`, where `1234567` is the channel id. Copy the channel id to the config file (`telegram -> private_file_channel`)

#### Set up a Telegram bot ([why do I need this?](#FAQ))

1. Go to [BotFather](https://telegram.me/BotFather), send `/newbot`, and follow the steps to create a bot.
2. Paste the bot token given by BotFater to the config file (`telegram -> bot -> token`)
3. Go to your file channel (created in the previous step), add your bot to subscriber, and promote it to admin, with permission to send/edit/delete messages.

## Config fields explanation

- telegram

- session_file: The file path to store the session data. If you want to use multiple accounts, you can set different session files for each account.
- account:
- session_file: The file path to store the session data. If you want to use multiple accounts, you can set different session files for each account.
- login_timeout: Time to wait before login attempt aborts (in milliseconds).

- tgfs

- users: the users authenticated by tgfs, used by both webdav authentication and monitor
- download
- porgress: Whether to show a progress bar when downloading files
- chunk_size_kb: The chunk size in KB when downloading files. Bigger chunk size means less requests.

- webdav
- host: The host of the WebDAV server listening on.
- port: The port of the WebDAV server listening on.
- users: The users of the WebDAV server.
- password: The password of the user.
- path: The root path for the WebDAV server. For example, setting this value to /webdav makes the WebDAV link `http://[host]:[port]/webdav`.

## FAQ

**Q: Why do I need a bot when my account is also able to send messages?**

Frequently sending messages may get your account banned, so using a bot is the best way to manage the risk. You can create another bot when it is banned.

**Q: Why do I need an account API then?**

The functionality of bot API is limited. For example, a bot can neither read history messages, nor send files exceeding 50MB. The account API is used when a bot cannot do the job.
12 changes: 7 additions & 5 deletions example-config.yaml
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
telegram:
api_id: your_api_id (mandatory, refer to README)
api_hash: your_api_hash (mandatory refer to README)
session_file: ~/.tgfs/your_account.session (optional, default to ~/.tgfs/account.session)
account:
api_id: your_api_id (mandatory, refer to README)
api_hash: your_api_hash (mandatory, refer to README)
session_file: ~/.tgfs/account.session
bot:
token: your_bot_token (mandatory, refer to README)
login_timeout: 300000
private_file_channel: your_channel_id (mandatory, refer to README)
public_file_channel: another_channel_id (optional)
public_file_channel: 0

tgfs:
users:
user:
password: password
download:
progress: true
chunk_size_kb: 1024

webdav:
Expand Down
10 changes: 6 additions & 4 deletions src/auth/account.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import * as fs from 'fs';

import { TelegramClient } from 'telegram';
import { StringSession } from 'telegram/sessions';

import * as fs from 'fs';
import * as input from 'input';

import { config } from 'src/config';
Expand All @@ -10,11 +11,12 @@ import { Logger } from 'src/utils/logger';
export const loginAsAccount = async (
reset: boolean = false,
): Promise<TelegramClient> => {
const apiId = config.telegram.reader.api_id;
const apiHash = config.telegram.reader.api_hash;
const session_file = config.telegram.reader.session_file;
const apiId = config.telegram.account.api_id;
const apiHash = config.telegram.account.api_hash;
const session_file = config.telegram.account.session_file;

if (!reset && fs.existsSync(session_file)) {
console.log(`using session file: ${session_file}`);
const session = new StringSession(String(fs.readFileSync(session_file)));
const client = new TelegramClient(session, apiId, apiHash, {
connectionRetries: 5,
Expand Down
28 changes: 13 additions & 15 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import fs from 'fs';

import input from 'input';
import yaml from 'js-yaml';
import os from 'os';
Expand All @@ -10,24 +11,21 @@ export const loadConfig = (configPath: string) => {
const file = fs.readFileSync(configPath, 'utf8');
const cfg = yaml.load(file);

const createSessionFileDir = (session_file: string) => {
if (session_file[0] === '~') {
session_file = path.join(os.homedir(), session_file.slice(1));
}
if (!fs.existsSync(session_file)) {
const dir = path.dirname(session_file);
fs.mkdirSync(dir, { recursive: true });
}
};
let session_file = cfg['telegram']['account']['session_file'];

createSessionFileDir(cfg['telegram']['account']['session_file']);
createSessionFileDir(cfg['telegram']['bot']['session_file']);
if (session_file[0] === '~') {
session_file = path.join(os.homedir(), session_file.slice(1));
}
if (!fs.existsSync(session_file)) {
const dir = path.dirname(session_file);
fs.mkdirSync(dir, { recursive: true });
}

config.telegram = {
account: {
api_id: cfg['telegram']['account']['api_id'],
api_hash: cfg['telegram']['account']['api_hash'],
session_file: cfg['telegram']['account']['session_file'],
session_file,
},
bot: {
token: cfg['telegram']['bot']['token'],
Expand Down Expand Up @@ -107,9 +105,9 @@ export const createConfig = async () => {
console.log(
'\nGo to https://t.me/botfather to create a Bot and paste the bot token here.',
);
config.telegram.bot.token = Number(
await input.text('Bot token', { validate: validateNotEmpty }),
);
config.telegram.bot.token = await input.text('Bot token', {
validate: validateNotEmpty,
});

console.log('\nCreate a PRIVATE channel and paste the channel id here');
config.telegram.private_file_channel = Number(
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ const { argv }: any = yargs(hideBin(process.argv))
try {
loadConfig(configPath);
} catch (err) {
Logger.debug(err);
configPath = await createConfig();
loadConfig(configPath);
}
Expand Down
2 changes: 0 additions & 2 deletions src/model/directory.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { Api } from 'telegram';

import { FileOrDirectoryAlreadyExistsError } from 'src/errors/path';

import { TGFSDirectoryObject, TGFSFileRefObject } from './message';
Expand Down
6 changes: 6 additions & 0 deletions src/utils/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ export class Logger {
return new Date(Date.now() - this.tzOffset).toISOString().slice(0, -1);
}

static debug(...args: any[]) {
if (process.env.DEBUG === 'true') {
console.debug(`[${this.getTime()}] [DEBUG]`, ...args);
}
}

static info(...args: any[]) {
console.info(`[${this.getTime()}] [INFO]`, ...args);
}
Expand Down

0 comments on commit 8320cac

Please sign in to comment.