1

I am using laravel-notification-channels/telegram I've implemented welcome notification for my users and my bot sending that message with no issue, the second part of my implementation is to add those users (who I've sent welcome message) into my channel.

PS: My bot is admin of my channel and has access to add users.

Logic

  1. Telegram users can register (done)
  2. Get telegram users data and send welcome message (done)
  3. Add telegram users to my channel by bot. (need help)

Code

Note: my code currently is just enough to send welcome message, nothing about adding users to channel...

public $user;
public $telegram_id;

/**
 * Create a new notification instance.
 *
 * @return void
 */
public function __construct($user, $telegram_id)
{
    $this->user = $user;
    $this->telegram_id = $telegram_id;
}

public function toTelegram($notifiable)
{
  // first message to bot (to recognize the user)
  TelegramMessage::create()
    ->to('MY_BOT_ID');

  // then message the user from bot
  return TelegramFile::create()
    ->to($this->telegram_id) // registered user ID on telegram
    ->content("Hello");
  }

  // (NEXT) add user to channel
  //.....

Any idea?

mafortis
  • 6,750
  • 23
  • 130
  • 288

2 Answers2

4

I'm afraid that won't be possible with the bot API since there's no API method for that (see Telegram Bot API).

However, you could give the users an invitation link to the chat so they can join. You can generate the invite link using exportChatInviteLink method, but this can't be done through laravel-notification-channels/telegram. Perhaps you could generate the link manually through the API, add it to your application, and then send the link to the users.

If you really wish to add the users to the chat, you can use the channels.inviteToChannel method on the client API. MadelineProto is a PHP Telegram client library which can do that.

sykez
  • 2,015
  • 15
  • 14
  • thanks for the answer, about first part `I'm afraid that won't be possible with the bot API` I don't believe that's fully correct since telegram allow bots to add users when you give them permission to. about mentioned package, the link you shared by `can do that` is just invitation link which I can create in my welcome message as well without help of this package, what I need is to join them automatically and not to send to send them invitation link. – mafortis Jan 13 '21 at 02:35
  • 3
    Although there's an *Add users* permission in Telegram, without the API method it still won't be able to do that. There are methods for kick, promote, and restrict on Bot API, but there is no API for adding user. You can verify this on their [Bot API](https://core.telegram.org/bots/api#available-methods). The MadelineProto package is a client API, not for Bot API, and the `channels.inviteToChannel` is the actual client API to invite users to a chat. It's not for the invite link. So the short answer is no, it's not possible with Bot API, but I've given you two alternatives. – sykez Jan 13 '21 at 11:50
  • Have you used that package yourself? If yes will be appreciate it if you can share how it's done in your answer – mafortis Jan 13 '21 at 11:53
  • 1
    No, I haven't used this package, but it's well documented with examples. – sykez Jan 13 '21 at 12:16
0

Added the corresponding method to the documentation

TELEGRAM DOCS

  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Gary Houbre Jun 13 '22 at 07:19