C
C
cannabioid13372021-01-22 18:07:29
PHP
cannabioid1337, 2021-01-22 18:07:29

How to send a message to a user by @username and not by chat_id?

I have a website with a form on it, you need a person to enter his @username in Telegram, and the bot sends him a message with a secret key. How to implement it via API?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
H
hesy, 2021-01-22
@hesy

1. You cannot send a message to a person on behalf of a bot if he has not interacted with him before.
2. Bot Api does not allow sending a message to a person by username, the exception is the channel.
https://core.telegram.org/bots/api#sendmessage chat_id
parameter :

Unique identifier for the target chat or username of the target channel (in the format @channelusername)

If the user has already communicated with the bot, then you can store the username and chat_id in the database, when entering in the username form, take the chat_id and send it using it.
Oh, and if I specify not my username, but Vasya?)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question