Skip to content

Commit

Permalink
Added get user id command
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitrijs-pavlovs-dev committed Dec 18, 2023
1 parent 0f30964 commit 556cc62
Show file tree
Hide file tree
Showing 7 changed files with 758 additions and 14 deletions.
4 changes: 3 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ POSTGRES_USER=admin
POSTGRES_DB=db
POSTGRES_PORT=23423
POSTGRES_HOST=localhost
WEB_URL=http://dest.domain.net/
WEB_URL=http://dest.domain.net/
TELEGRAM_APP_ID=1111111111
TELEGRAM_APP_ID_HASH=XXXXXXXXXXXXXXXXXXXX
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ jobs:
echo "POSTGRES_HOST=${{ secrets.POSTGRES_HOST }}" >> .env
echo "POSTGRES_PORT=${{ secrets.POSTGRES_PORT }}" >> .env
echo "WEB_URL=${{ secrets.WEB_URL }}" >> .env
echo "TELEGRAM_APP_ID=${{ secrets.TELEGRAM_APP_ID }}" >> .env
echo "TELEGRAM_APP_ID_HASH=${{ secrets.TELEGRAM_APP_ID_HASH }}" >> .env
- name: Copy docker-compose file and .env file to remote server
uses: appleboy/scp-action@master
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Preview this bot in telegram https://t.me/WhaleTestAppBot

- `/start` Command: Users can initiate interaction with the bot. The bot will respond with a message and a web app button that, when clicked, displays the user's first name as a greeting on a simple webpage.
- `/adminhello <telegram_id> <text>` Command: Admins can send personalized messages to users by invoking this command followed by the user's Telegram ID and the intended message.
- `/getuserid <username>` Admins can retrivies any telegram user's id by providing username
- User Persistence: The bot stores user information in a PostgresSQL database, making it accessible for future interactions and additional functionality.

### Project Structure
Expand Down
3 changes: 2 additions & 1 deletion apps/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"reflect-metadata": "^0.1.13",
"rxjs": "^7.8.0",
"telegraf": "^4.15.3",
"telegram": "^2.19.10",
"typeorm": "^0.3.17"
},
"devDependencies": {
Expand Down Expand Up @@ -73,4 +74,4 @@
"coverageDirectory": "../coverage",
"testEnvironment": "node"
}
}
}
52 changes: 52 additions & 0 deletions apps/backend/src/services/bot.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import { TelegramUser } from '../entities/telegram-user.entity';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { ConfigService } from '@nestjs/config';
import { TelegramClient } from 'telegram';
import { StringSession } from 'telegram/sessions';
import { Api } from 'telegram/tl';
import input from 'input'; // npm i input

@Update()
export class BotService {
Expand Down Expand Up @@ -58,4 +62,52 @@ export class BotService {
await ctx.reply('This command requires a text message.');
}
}

@Command('getuserid')
async getUserId(@Ctx() ctx: Context) {
const user = await this.userRepository.findOne({ where: { id: ctx.from.id } });

// Check if the user is an admin
if (!(user && user.is_admin)) {
await ctx.reply('You are not authorized to use this command.');
return;
}

if ('text' in ctx.message) {
const args = ctx.message.text.split(' ').slice(1);
const username = args[0]; // Assuming this is the Telegram username

// Ensure that username is provided
if (!username) {
await ctx.reply('Please provide a username.');
return;
}

const stringSession = ''; // Your saved session string
const botToken = this.configService.get<string>('TELEGRAM_BOT_TOKEN');
const appId = this.configService.get<number>('TELEGRAM_APP_ID');
const appIdHash = this.configService.get<string>('TELEGRAM_APP_ID_HASH');
(async () => {
const client = new TelegramClient(new StringSession(stringSession), Number(appId), appIdHash, {
connectionRetries: 5,
});
await client.start({
botAuthToken: botToken,
});
console.log('Session:', client.session.save());

try {
const userEntity = await client.getInputEntity(username);
if ('userId' in userEntity) {
await ctx.reply('User ID: ' + userEntity.userId);
}
} catch (error) {
console.error('Error retrieving user entity:', error);
await ctx.reply('Error retrieving user information.');
}
})();
} else {
await ctx.reply('This command requires a text message.');
}
}
}
2 changes: 2 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ services:
- POSTGRES_USER=${POSTGRES_USER}
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
- POSTGRES_DB=${POSTGRES_DB}
- TELEGRAM_APP_ID=${TELEGRAM_APP_ID}
- TELEGRAM_APP_ID_HASH=${TELEGRAM_APP_ID_HASH}
- WEB_URL=${WEB_URL}
web:
image: dayrim/whale-web:latest
Expand Down
Loading

0 comments on commit 556cc62

Please sign in to comment.