NeuroAgent

How to Get Phone Number from Telegram in n8n: Complete Guide

Learn how to properly configure getting phone numbers from Telegram in n8n. Solving issues with callback requests and Wait for Webhook for saving contacts.

Question

How do I get and save a phone number from Telegram in n8n? I’m using n8n locally for learning purposes. The issue is that I’m sending a keyboard with request_number=true to request a phone number, but I don’t understand how to get this number. When I click “Share number” in Telegram, the process in n8n already completes and doesn’t wait for my response. Wait nodes also don’t help as they return old responses without the contact. How do I properly configure getting phone numbers in n8n when working with Telegram?

NeuroAgent

Getting Phone Numbers from Telegram in n8n

To retrieve a phone number from Telegram in n8n, you need to use the correct combination of Telegram Trigger and Wait for Webhook nodes, configuring them to handle callback requests when a contact is shared. The key issue is that Telegram sends a callback request immediately when the “Share number” button is pressed, and n8n must be configured to handle this rather than waiting for a regular message.

Contents

Basic Approach to Getting Phone Numbers

To get a phone number from Telegram in n8n, you need to understand how request_number buttons work. When a user presses “Share number”, Telegram sends a callback request, not a regular message. Therefore, the standard Telegram Trigger won’t work properly.

The correct approach includes:

  1. Using a Telegram Trigger configured to listen for callback requests
  2. Setting up Wait for Webhook to wait for the response from Telegram
  3. Processing the received callback request and extracting the phone number
  4. Saving the number in the required format

Important: When setting request_number=true in inline keyboard, Telegram sends a callback_query that contains information about the button press but not the phone number itself. The phone number needs to be requested separately after receiving the callback.

Telegram Trigger Setup

To properly set up phone number retrieval in n8n, follow these steps:

  1. Add a Telegram Trigger node to the canvas

  2. In the node settings, select the “On Trigger” operation

  3. In the “Choose Trigger” section, set:

    • Bot Token (your Telegram bot token)
    • Trigger Type: “Callback Query”
    • Webhook URL: your local URL for receiving webhooks (e.g., http://localhost:5678/webhook/telegram)
  4. In Security settings:

    • Check the “Validate Certificate” box
    • In the “Secret” field, enter a secret key for webhook signing
json
{
  "operation": "onTrigger",
  "parameters": {
    "botToken": "your_bot_token",
    "webhookUrl": "http://localhost:5678/webhook/telegram",
    "secret": "your_secret_key",
    "validateCertificate": true
  }
}

Using Wait for Webhook

After receiving a callback request, you need to wait for the response from Telegram with the phone number:

  1. Add a Wait for Webhook node after the Telegram Trigger

  2. Configure its parameters:

    • Webhook Name: telegram_phone_response (unique name)
    • Timeout: 30000 (30 seconds)
    • Continue On Timeout: false
  3. In the “Response Type” section, select “JSON”

  4. In the “Response Mapping” settings, specify the path to the phone number data

json
{
  "operation": "waitForWebhook",
  "parameters": {
    "webhookName": "telegram_phone_response",
    "timeout": 30000,
    "continueOnTimeout": false,
    "responseType": "json",
    "responseMapping": {
      "phoneNumber": "$.data.contact.phone_number"
    }
  }
}

Callback Request Handling

When receiving a callback request from Telegram containing information about the “Share number” button press, you need to:

  1. Identify the callback: The callback_query data will contain a data field with information about the pressed button
  2. Send a request to get the contact: Use Telegram API’s getContact method
  3. Process the received contact: Extract the phone number from the contact object

Example processing in a Set node:

json
{
  "operation": "set",
  "parameters": {
    "values": {
      "callbackId": "={{ $json.callback_query.id }}",
      "chatId": "={{ $json.callback_query.message.chat.id }}",
      "buttonData": "={{ $json.callback_query.data }}",
      "requestContact": "={{ $json.callback_query.data.includes('request_contact') }}"
    }
  }
}

Then use an HTTP Request node to call the Telegram API method:

json
{
  "operation": "httpRequest",
  "parameters": {
    "url": "https://api.telegram.org/bot{{ $credentials.telegramBotToken }}/getContact",
    "method": "POST",
    "headers": {
      "Content-Type": "application/json"
    },
    "body": {
      "chat_id": "={{ $json.chatId }}",
      "user_id": "={{ $json.callback_query.from.id }}"
    }
  }
}

Complete Workflow Example

Here’s a complete workflow example for getting a phone number:

  1. Telegram Trigger (configured for callback_query)
  2. Set (extracting data from callback)
  3. HTTP Request (getting contact via Telegram API)
  4. Set (saving phone number)
  5. HTTP Response (sending response to user)
mermaid
graph TD
    A[Telegram Trigger] --> B[Set - Data Extraction]
    B --> C{Has callback?}
    C -->|Yes| D[HTTP Request - getContact]
    C -->|No| E[Waiting for response]
    D --> F[Set - Saving Number]
    F --> G[HTTP Response - Confirmation]
    G --> A

Setup Steps:

  1. Telegram Trigger

    • Bot Token: your token
    • Trigger Type: Callback Query
    • Webhook URL: your local URL
  2. Set Node 1

    json
    {
      "callbackId": "={{ $json.callback_query.id }}",
      "chatId": "={{ $json.callback_query.message.chat.id }}",
      "buttonData": "={{ $json.callback_query.data }}"
    }
    
  3. HTTP Request Node

    json
    {
      "url": "https://api.telegram.org/bot{{ $credentials.telegramBotToken }}/getChat",
      "method": "POST",
      "body": {
        "chat_id": "={{ $json.chatId }}",
        "user_id": "={{ $json.callback_query.from.id }}"
      }
    }
    
  4. Set Node 2

    json
    {
      "phoneNumber": "={{ $json.result.contact.phone_number }}",
      "firstName": "={{ $json.result.contact.first_name }}",
      "lastName": "={{ $json.result.contact.last_name }}"
    }
    
  5. HTTP Response Node

    json
    {
      "callbackQueryId": "={{ $json.callbackId }}",
      "text": "Thank you! Your phone number has been saved."
    }
    

Common Issues

Issue 1: Process terminates before receiving the number

Solution: Use Wait for Webhook with proper callback configuration. Ensure your Telegram Trigger is set up to receive callback_query, not regular messages.

Issue 2: Wait returns old response

Solution: Configure Wait for Webhook for a specific callback by using a unique webhook name for each request.

Issue 3: Multiple triggers don’t work

Solution: As noted in the n8n documentation, due to Telegram API limitations, you can only use one Telegram Trigger for each bot simultaneously. Use one trigger with different processing types.

Issue 4: Phone number not extracted

Solution: Check the structure of the Telegram API response. The phone number is typically located in $json.result.contact.phone_number when using the getChat method.

Alternative Methods

Method 1: Using Contact Sharing

If the standard method doesn’t work, you can use the built-in contact sharing feature:

  1. Create an inline keyboard with a “Share contact” button
  2. In the button settings, set request_contact: true
  3. Configure Telegram Trigger to receive messages with contacts
json
{
  "inline_keyboard": [
    [
      {
        "text": "Share number",
        "request_contact": true,
        "callback_data": "share_contact"
      }
    ]
  ]
}

Method 2: Using Custom Forms

As mentioned in the n8n documentation, you can use custom forms:

  1. Create a form with a phone number input field
  2. Use Telegram Trigger to receive messages
  3. Validate the phone number format in n8n

Method 3: HTTP Request + Telegram API

For more complex scenarios, you can directly call the Telegram API via the HTTP Request node:

json
{
  "operation": "httpRequest",
  "parameters": {
    "url": "https://api.telegram.org/bot{{ $credentials.telegramBotToken }}/sendMessage",
    "method": "POST",
    "body": {
      "chat_id": "={{ $json.chat_id }}",
      "text": "Please share your phone number",
      "reply_markup": {
        "keyboard": [
          [
            {
              "text": "Share number",
              "request_contact": true
            }
          ]
        ],
        "one_time_keyboard": true,
        "resize_keyboard": true
      }
    }
  }
}

Conclusion

Getting phone numbers from Telegram in n8n requires proper configuration of callback request handling. Key points:

  1. Use the correct Trigger: Configure Telegram Trigger to receive callback_query, not regular messages
  2. Handle callbacks immediately: When a user presses “Share number”, Telegram sends a callback that needs to be processed right away
  3. Use Wait for Webhook: To wait for contact data response
  4. Process data correctly: Extract the phone number from the Telegram API response
  5. Consider API limitations: Remember that only one Telegram Trigger can be used per bot

For local development, ensure your n8n server is accessible from the internet or use ngrok to create a public URL for the webhook. This will allow Telegram to send callback requests to your local n8n instance.

Sources

  1. Telegram node Callback operations documentation | n8n Docs
  2. Telegram - can’t use query buttons and callbacks - by design - Questions - n8n Community
  3. Ask a question via Telegram and wait for response? - Questions - n8n Community
  4. Telegram node Message operations documentation | n8n Docs
  5. n8n Telegram Bot Integration​ - MpireSolutions