Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open Telegram chat (with a Bot) from a iOS app

I'm trying to open Telegram from my app, for the users to talk with a bot I made. So far, it is working but the only way I found to get the bot chat opened was using the https://telegram.me/MyBot url. But this way, it opens Safari, and then the user is asked if he wants to open it on the Telegram app. Initially it was asking one time, and then, after the first time, it was just passing thru safari and opening Telegram automatically. But it stopped and now, every single time it loads Safari and some times, it even doesn't shows the popup asking the user if it can open the Telegram app.

Is there any way to use that 'tg://' url (that should open directly the Telegram app) to open a chat with a bot? Only saw working examples with phone numbers. Tried in different ways but no success at all...

Any help would be great.

Thanks in advance!

like image 479
Lucas Almeida Avatar asked Nov 14 '16 18:11

Lucas Almeida


People also ask

Is Telegram open source for IOS?

Our apps are open source and support reproducible builds. This means that anyone can independently verify that our code on GitHub is the exact same code that was used to build the apps you download from App Store or Google Play.

What is BotFather Telegram?

- [Instructor] A Telegram Bot works by sending messages between your server and Telegram's. The system is secure because the two share a secret code known as a Bot Token. You get this token by interacting with a special Telegram user known rather amusingly as The BotFather.


1 Answers

Swift 3/4/5+

This is exactly what you looking for:

let botURL = URL.init(string: "tg://resolve?domain=MyBot")

if UIApplication.shared.canOpenURL(botURL!) {
    UIApplication.shared.openURL(botURL!)
} else {
  // Telegram is not installed.
}

Don't forget to add URI schema's of Telegram to info.plist:

<key>LSApplicationQueriesSchemes</key>
<array>
   <string>tg</string>
</array>
like image 99
mathema Avatar answered Sep 21 '22 20:09

mathema