Open Telegram chat (with a Bot) from a iOS app
Asked Answered
D

4

12

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!

Dinothere answered 14/11, 2016 at 18:25 Comment(0)
A
26

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>
Appanage answered 9/1, 2017 at 14:40 Comment(1)
I have this issue, and user your suggest code but when open link i'm get this error in telegram : Sorry this user doesn't seem to be exist. but can open this URL from android application.Coagulant
S
7

For Swift 4.2+ and iOS 9+

let screenName =  "und3rflow" // <<< ONLY CHANGE THIS ID
let appURL = NSURL(string: "tg://resolve?domain=\(screenName)")!
let webURL = NSURL(string: "https://t.me/\(screenName)")!
if UIApplication.shared.canOpenURL(appURL as URL) {
    if #available(iOS 10.0, *) {
        UIApplication.shared.open(appURL as URL, options: [:], completionHandler: nil)
    }
    else {
        UIApplication.shared.openURL(appURL as URL)
    }
}
else {
    //redirect to safari because user doesn't have Telegram
    if #available(iOS 10.0, *) {
        UIApplication.shared.open(webURL as URL, options: [:], completionHandler: nil)
    }
    else {
        UIApplication.shared.openURL(webURL as URL)
    }
}
Shum answered 4/11, 2018 at 11:37 Comment(0)
F
2
  1. open info.plist and add LSApplicationQueriesSchemes in this .plist change type from string to array and add key = item0 , value = telegram
  2. write this code for open telegram

    let url = URL(string: "instagram://user?username=fastteb")
    if(UIApplication.shared.canOpenURL(url!))
    {
        UIApplication.shared.open(url!, options: [:], completionHandler: nil)
    }else
    {
        let alert = UIAlertController(title: "Error", message: "you don't have instagram,you need to install instagram", preferredStyle: .alert)
        let action = UIAlertAction(title: "Download And Install", style: .default, handler: { (UIAlertAction) in
            let urlAppStore = URL(string: "itms-apps://itunes.apple.com/app/id389801252")
            if(UIApplication.shared.canOpenURL(urlAppStore!))
            {
                UIApplication.shared.open(urlAppStore!, options: [:], completionHandler: nil)
            }
    
        })
        let actionCancel = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
        alert.addAction(action)
        alert.addAction(actionCancel)
        self.present(alert, animated: true, completion: nil)
    
    
    
    
    }
    
Fajardo answered 15/9, 2018 at 20:24 Comment(0)
M
0

You can open tg through the link

let botURL = URL.init(string: "https://t.me/\("bot_or_user_name")")

    if UIApplication.shared.canOpenURL(botURL!) {
        UIApplication.shared.openURL(botURL!)
    } else {
        let urlAppStore = URL(string: "itms-apps://itunes.apple.com/app/id686449807")
        if(UIApplication.shared.canOpenURL(urlAppStore!))
        {
            if #available(iOS 10.0, *) {
                UIApplication.shared.open(urlAppStore!, options: [:], completionHandler: nil)
            } else {
                UIApplication.shared.openURL(urlAppStore!)
            }
        }
    }
Mimetic answered 6/9, 2019 at 9:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.