【问题标题】:Open Telegram chat (with a Bot) from a iOS app从 iOS 应用程序打开 Telegram 聊天(使用机器人)
【发布时间】:2017-03-28 11:32:39
【问题描述】:

我正在尝试从我的应用中打开 Telegram,以便用户与我制作的机器人交谈。到目前为止,它正在工作,但我发现打开机器人聊天的唯一方法是使用https://telegram.me/MyBot url。但是这样一来,它会打开 Safari,然后询问用户是否要在 Telegram 应用程序上打开它。最初它只是询问一次,然后,在第一次之后,它只是通过 safari 并自动打开 Telegram。但它停止了,现在,每次加载 Safari 时,它甚至没有显示询问用户是否可以打开 Telegram 应用程序的弹出窗口。

有什么方法可以使用 'tg://' url(应该直接打开 Telegram 应用程序)来打开与机器人的聊天?只看到带有电话号码的工作示例。尝试了不同的方法,但都没有成功...

任何帮助都会很棒。

提前致谢!

【问题讨论】:

    标签: ios swift3 telegram telegram-bot


    【解决方案1】:

    斯威夫特 3/4/5+

    这正是您要找的:

    let botURL = URL.init(string: "tg://resolve?domain=MyBot")
    
    if UIApplication.shared.canOpenURL(botURL!) {
        UIApplication.shared.openURL(botURL!)
    } else {
      // Telegram is not installed.
    }
    

    不要忘记将 Telegram 的 URI 模式添加到 info.plist:

    <key>LSApplicationQueriesSchemes</key>
    <array>
       <string>tg</string>
    </array>
    

    【讨论】:

    • 我有这个问题,并使用您的建议代码,但是当打开链接时,我在电报中收到此错误:抱歉,此用户似乎不存在。但可以从 android 应用程序打开此 URL。
    【解决方案2】:

    适用于 Swift 4.2+ 和 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)
        }
    }
    

    【讨论】:

      【解决方案3】:
      1. 打开 info.plist 并在此 .plist 中添加 LSApplicationQueriesSchemes 将类型从字符串更改为数组并添加 key = item0 , value = telegram
      2. 为公开电报编写此代码

        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)
        
        
        
        
        }
        

      【讨论】:

        【解决方案4】:

        可以通过链接打开tg

        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!)
                    }
                }
            }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-01-21
          • 2021-10-01
          • 1970-01-01
          相关资源
          最近更新 更多