Mohamadjavad Vakili
Mohamadjavad Vakili

Reputation: 3348

How to send a message to the user who logged in through the telegram in website?

How to send a message to the user who logged in through the telegram in website?

Telegram Login for Websites

Telegram bots are a powerful communication tool, but until today they couldn't start a conversation. Even if you wanted them to reach out to you, you had to chat them up first.

and

Bot API 3.6

Added the new field connected_website to Message. The bot will receive a message with this field in a private chat when a user logs in on the bot's connected website using the Login Widget and allows sending messages from your bot.

After login user,my bot can not send message with use user_id,Even the OnMessage event is not fired.

How start a conversation by bot?

Thankful.

Upvotes: 3

Views: 2415

Answers (2)

erhosen
erhosen

Reputation: 403

In my case I did not receive an update event as a webhook, like Ali Titan described. But instead chat_id came to custom onauth js funcion:

{
    "id': 100102379, 
    "first_name": "Guido", 
    "last_name": "Van Rossum", 
    "username": "guido",
    "auth_date": 1642941713, 
    "hash": "568a5665500a389a754a1c04348ea1c0434d4b507b1920f3ca1ff017a1c04341"
}

In this example id is exactly chat_id you are looking for.

In other words, if you want to provide it to your backend, you should write something like:

<script async src="https://telegram.org/js/telegram-widget.js?15" data-telegram-login="samplebot" data-size="large" data-onauth="onTelegramAuth(user)" data-request-access="write"></script>
<script type="text/javascript">
    function onTelegramAuth(user) {
        $.ajax({
            url: '/telegram_widget/login/',
            method: 'POST',
            contentType: "application/json; charset=utf-8",
            data: JSON.stringify(user),
        });
    }
</script>

And then use this id to communicate with your user.

Upvotes: 2

Ali Titan
Ali Titan

Reputation: 41

You answered your question yourself here :

Added the new field connected_website to Message. The bot will receive a message with this field in a private chat when a user logs in on the bot's connected website using the Login Widget and allows sending messages from your bot.

More descriptions: After the user logged in with telegram login widget,you should wait for an update event (assuming you'r using Webhook method for your telegram bot). the update body should be something like this:

{
"update_id": 290285,
"message": {
    "message_id": 12,
    "from": {
        "id": 117854,
        "is_bot": false,
        "first_name": "",
        "last_name": "",
        "language_code": "fa"
    },
    "date": 1549829158,
    "chat": {
        "id": "you need this to start conversation",
        "type": "private",
        "first_name": "user_firstname",
        "last_name": "user_last name"
    },
    "connected_website": "the domain mapped to your bot"
}

from the "connected_website" filled you can realize this is a user logged in for the first time and save the chat id to start next conversations in future. PS : I wonder why there is no documentation about this at telegram or at least I didn't found anything.

Upvotes: 4

Related Questions