Bringing your SMS contacts over to Telegram

Due to TextIt’s multi-channel capabilities, it is straightforward to configure your bot to operate simultaneously over SMS and chat applications such as Telegram. Expanding your bot to Telegram also presents an effective way to reduce messaging costs. This brief guide explains how you can encourage your SMS contacts to transition to your Telegram bot while maintaining their complete interaction history within your database.

Channels and URNs

When a contact initiates communication on a new channel, a separate contact entry is generated. Each contact is assigned a unique address for the channel they are using, known as a URN. Consequently, if a contact uses both SMS and Telegram, two distinct entries will exist: one with an SMS URN (a phone number) and another with a Telegram URN (a numeric identifier with an optional display name). The objective is to merge these URNs under a single contact to preserve a unified history.

Consider a scenario where you wish to migrate an SMS subscriber to Telegram. To achieve a single contact with two URNs, follow this approach:

Steps

  • Dispatch an SMS containing a link to your Telegram bot.

  • The contact clicks the link, initiating a Telegram chat session.

  • The contact receives an SMS prompting them to confirm the switch to Telegram.

  • They confirm by replying “yes,” at which point we add their new Telegram URN to their existing contact and set it as the preferred channel.

Dispatching the Invitation

To send the invitation, text them a link to your Telegram bot. You can obtain this link by accessing your bot within the Telegram interface and selecting the share option.

Your message might resemble:

Reduce messaging fees by switching to our Telegram bot. Simply click here: https://t.me/PurringtonBot?start=phone@(text_slice((urn_parts(contact.urn).path), 1))

To embed the contact’s current phone number in the invite, extract the number from their URN using the urn_parts() function to access the URN path (the phone number).

@(urn_parts(contact.urn).path)

This returns a value like +12065551212. Since Telegram requires the plus symbol to be omitted, trim the first character using the text_slice function.

@(text_slice((urn_parts(contact.urn).path), 1))

Processing the Invitation Response

Within your Telegram bot’s /start flow, differentiate between a standard join and an invite link by checking if the start command begins with /start phone. If a phone number is provided, initiate a flow for that number to confirm the details and add the new Telegram ID.

When the start button is clicked, a command like /start phone12065551212 is passed. Extract the phone number using:

@(text_slice(input, 12))

This yields just the phone number (e.g., 12065551212). Then, use the Start Somebody Else action to initiate the confirmation flow for that number.

Incorporating the New Telegram ID

In the confirmation flow, prompt the contact to confirm their intent. Upon receiving a “yes” response, retrieve their Telegram ID and save it to their SMS contact.

Verification is crucial to prevent unauthorized account linking. Without it, someone could initiate the bot with any phone number and potentially gain access to a contact not their own. While using a random code instead of a phone number is an alternative, this example uses phone numbers for simplicity. Proper confirmation ensures security against malicious attempts.

Here, since the flow was triggered by their Telegram contact, we reference the initiating contact using @parent.contact and their URN with @parent.contact.urn, which returns a value like telegram:1234567#norbertk. The actual ID is the number between the : and the #. Access it using:

@(urn_parts(parent.contact.urn).path)

Telegram users may also have an optional display name following the #. To include this, use:

@(urn_parts(parent.contact.urn).display)

Combine these into a single expression:

@(urn_parts(parent.contact.urn).path)#@(urn_parts(parent.contact.urn).display)

That’s all there is to it!

If you have any questions about implementing this, please don’t hesitate to reach out. We’re here to help.