44

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?

HoldOffHunger
  • 18,769
  • 10
  • 104
  • 133
farhad1985
  • 551
  • 1
  • 4
  • 8

13 Answers13

74

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>

Mohebifar
  • 3,341
  • 1
  • 24
  • 32
18

This works with me:

tg://msg?text=Mi_mensaje&to=+1555999
Mariam
  • 683
  • 1
  • 11
  • 27
  • How to check isTelegram installed? in swift – Qadir Hussain Apr 08 '16 at 11:45
  • 1
    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_bot – Qadir Hussain Jul 20 '16 at 06:28
  • @QadirHussain to open telegram to start chat with bot use url like 'https://telegram.me/my_bot' . it redirects you to chat screen with that bot. – Hos Ap Jan 30 '17 at 23:21
  • 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. – gowthami Jan 21 '21 at 07:51
  • I have the userId ( not a username ) like 854845663 How can I use it instead of a username? – Mo Farhand Jan 22 '22 at 03:50
15

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

HoldOffHunger
  • 18,769
  • 10
  • 104
  • 133
6

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.

Francis Rodrigues
  • 1,470
  • 4
  • 25
  • 61
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>
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.

gowthami
  • 155
  • 1
  • 10
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>
1

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

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.

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.

f0xik
  • 101
  • 5
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)
Vito Valov
  • 1,765
  • 1
  • 19
  • 37
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

Daniel Abril
  • 418
  • 3
  • 11
0

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

<a href="https://t.me/targetedusername">
Muhammad Faisal
  • 734
  • 10
  • 24