Share a link via URL scheme to Telegram
Asked Answered
B

13

46

I want to share a link via URL scheme for Telegram.

I have created this:

tg://msg?text = www.example.com?t=12

The link, opens telegram but nothing else happens.

I have used the same code for Viber, and it works:

viber://forward?text = www.example.com?t=12

And it opens a new message in Viber with this text:

www.example.com

In the other words, it cuts my URL.

Any ideas?

Build answered 11/7, 2015 at 10:47 Comment(0)
E
77

You can also use telegram.me share link which falls back to webogram if a telegram app is not installed on the device.

https://telegram.me/share/url?url=<URL>&text=<TEXT>

Edulcorate answered 9/2, 2016 at 11:7 Comment(5)
can we open telegram to start chat with bot selected (not a number) i.e something like this tg://msg?text=HI there&to=@my_botBienne
@Qadir Hussain have you found any solution yet? contact added in phone contact, i use tg://msg?text=hi&to=+91XX then ios redirect to user chat screen, how cant i do in android?Wirewove
@Maulikpatel this should help for android #21627667Bienne
@QadirHussain it will redirect me to telegram but not particular user chat screen.Wirewove
@Maulikpatel don't know about this in androidBienne
B
18

This works with me:

tg://msg?text=Mi_mensaje&to=+1555999
Brost answered 28/12, 2015 at 19:14 Comment(5)
How to check isTelegram installed? in swiftBienne
can we open telegram to start chat with bot selected (not a number) i.e something like this tg://msg?text=Mi_mensaje&to=@my_botBienne
@QadirHussain to open telegram to start chat with bot use url like 'telegram.me/my_bot' . it redirects you to chat screen with that bot.Heterogynous
<a href = "tg://msg?text=Mi_mensaje&to=+1555999"><telegram</a> this is opening 'xdg' and after we should select contact. Is there any solution to open xdg and then contact and having sending message. thanks in advance.Industrious
I have the userId ( not a username ) like 854845663 How can I use it instead of a username?Hamitic
C
16

You have the following options for a URL...

https://t.me/share/url?url={url}&text={text}
https://telegram.me/share/url?url={url}&text={text}
tg://msg_url?url={url}&text={text}

In case you want to confirm, here is the official API source: Core.Telegram.org: Widgets -> Sharing Button.

If you are interested in watching a project that keeps track of these URLs, then check us out!: https://github.com/bradvin/social-share-urls#telegramme

Social Share URLs

Coquina answered 23/4, 2018 at 14:15 Comment(0)
A
7

You can use the link telegram.me which will provide a preview page with an alert requesting to open the link in the application.

https://telegram.me/share/url?url=<URL>&text=<TEXT>

enter image description here

The second option is calling the application link directly:

tg://msg_url?url=<url>&text=<encoded-text>

I particularly prefer the second option, which also works on desktop applications.

Aluminous answered 25/2, 2020 at 7:4 Comment(0)
R
5

For Telegram share:

Objective C:

if([UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"tg://msg?text=test"]){
 [UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tg://msg?text=test"]
}else{
 //App not installed.
}

Swift 3.0:

let urlString = "tg://msg?text=test"
let tgUrl = URL.init(string:urlString.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed)!)
if UIApplication.shared.canOpenURL(tgUrl!)
    {
        UIApplication.shared.openURL(tgUrl!)
    }else
    {
       //App not installed.
    }

If you have used canOpenURL, then need to add in info.plist

<key>LSApplicationQueriesSchemes</key>
<array>
   <string>tg</string>
</array>
Recourse answered 14/12, 2016 at 11:14 Comment(0)
I
4
<a href = "https://telegram.me/share/url?url=<URL>&text=<TEXT>">Telegram</a>

with this we can open xdg of telegram and if we select contact , by default sending text will come in message field.

Industrious answered 21/1, 2021 at 7:57 Comment(0)
E
3

PHP

<a href="tg://msg?text=<?php echo rawurlencode($gotoURL); ?>">Link</a>


JavaScript

<script>TEXT="any text or url";</script>

<a onclick="window.location='tg://msg?text='+encodeURIComponent(TEXT);">Link</a>
Egidio answered 15/12, 2015 at 16:27 Comment(0)
B
1

Maybe you use localhost therefore it does not show share. try it in live host

Bartel answered 6/8, 2020 at 10:54 Comment(0)
M
1

You should stop using the protocol for the desktop applications because it does not work on the evolving web: it does not work on Web apps, ChromeOS, or some mobile devices.

Always use the NEW way: https://t.me because it will open the Telegram DESKTOP app in Windows\Linux\Mac IF THE USER WANTS\HAS it, otherwise it will open the WEB page\web app which is THE WAY.

  1. https://web.telegram.org/k
  2. https://web.telegram.org/z

Telegram has TWO web apps that have been building out for the past year!

Both are great, adding TONs of parity, and have different approaches.

Melliemelliferous answered 6/11, 2022 at 3:46 Comment(0)
C
0

To check if the Telegram is installed you can do the following (borrowed from the Whatsapp sharer module of ShareKit):

BOOL isTelegramInstalled = [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"tg://msg?text=test"]];

iOS checks if there's any app installed which can handle the tg:// scheme, which is Telegram.

Confrere answered 30/10, 2016 at 14:28 Comment(0)
H
0

Just tested, this way it works both opening telegram app or browser in case it's not installed:

let webURL = NSURL(string: "https://t.me/<YOUR ID>")!
UIApplication.shared.open(webURL as URL)
Hildick answered 25/6, 2019 at 19:32 Comment(0)
C
0

Your have two problems:

  1. On one hand you are using the wrong scheme for sharing URLs, the correct one is msg_url;
  2. On the other hand you are trying to share a URL with parameters. You need to encode your ? in order to make it work. The percent code is %3F
  3. Extra Tip: Also if you want it to be a link once shared you should include the HTTPS:// encoded of course.

Try this and you'll see it works fine: tg://msg_url?url=https%3A%2F%2Fwww.example.com%3Ft=12

Choe answered 28/11, 2021 at 20:14 Comment(0)
T
0

If you want to open chat with bot or people, just write this simple code

<a href="https://t.me/targetedusername">
Threonine answered 20/6, 2022 at 3:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.