telegramtelegram-apiwtelegramclient

WTelegramClient ask api hash every time instead of login from session


I have a cron application with jobs, and every job starts with creating the client and calling the method LoginUserIfNeeded(). And I need to provide an API hash every time the job starts, but I want to login once and then every other time from the session file.

Ideally, I don't want to provide an API hash on production server. I just want to add session data and run the jobs where I'm using Telegram client.

public async Task<Unit> Handle(CreateChannelPostCommand command, CancellationToken cancellationToken)
    {
        using var tgClient = new WTelegram.Client();
        //Ask for an API hash every time a job starts
        await tgClient.LoginUserIfNeeded();

        var chats = await tgClient.Messages_GetAllChats();
        var chat = chats.chats[command.TlChannelId];
        
        var message = await tgClient.SendMessageAsync(chat, $"title: {command.Title}, body: {command.Body}");

        var advertisement = await _advertisementRepository.GetByIdAsync(command.AdvertisementId);
        advertisement.ChannelPostId = message.ID;
        advertisement.ChannelPostLink = GeneratePostLink(message);
        
        _advertisementRepository.Update(advertisement);
        await _unitOfWork.SaveChangesAsync(cancellationToken);
        
        return Unit.Value;
    }

Is it possible to do something like this, or do I need to login manually once and let the client class live until the application stops?


Solution

  • If you're using library, it should create a file in your working directory that will execute API calls with your identity (telegram account). The file is personal and is called every time you execute your program file. hence you would not have to provide api/hash IDs