Jan-Lukas Else

Thoughts of an IT expert

Ditching Pocket Premium: Streamline link saving with Telegram

Published on in 👨‍💻 Dev
Updated on
Short link: https://b.jlel.se/s/6ca
AI generated summary: The blog post discusses how to streamline link saving by using Telegram as an alternative to Pocket Premium, providing step-by-step instructions on how to set it up using a Telegram bot and a userscript manager like Violentmonkey.

For the past two years, I’ve been using Pocket to save links that I want to revisit later. However, as my yearly subscription is about to expire, I’ve started thinking about finding a free alternative that offers the features I actually use, without paying for unnecessary extras.

Although Pocket Premium has a wide range of features, I primarily use it as a place to dump links and synchronize them between my PC and phone. For these purposes, I don’t really need the full functionality provided by Pocket Premium.

After considering my needs, I came up with a simpler solution. I can use Telegram to save links by sharing them to a private Telegram channel. On my phone using the Telegram app and on my desktop browser using a userscript.

Here’s how you can set it up:

  1. Create a Telegram bot using the BotFather. The BotFather will provide you with an API token for the bot.
  2. Create a private channel on Telegram and invite the bot you just created to the channel with permission to send messages.
  3. Obtain the chat ID of the channel. You can do this by sending a message to the channel, then copying the share link for the message. The share link has the structure of https://t.me/c/<CHANNEL_ID>/<MESSAGE_ID>. Please note that the Telegram Bot API requires the -100 prefix for private channels. Without it, you’ll encounter an error stating that the chat was not found. For public channels, you can simply use @channelname instead.

For the userscript, you can use a userscript manager like Violentmonkey. This userscript will allow you to send the current page link and title to your private Telegram channel directly from your browser. Follow these steps:

  1. Install the Violentmonkey extension for your browser.
  2. Click on the Violentmonkey icon in your browser toolbar and select “Create a new script”.
  3. Replace the existing code in the script editor with the JavaScript code provided below.
  4. Modify the botApiToken and chatId variables with your Telegram bot API token and channel ID.
  5. Save the script.

Here’s the JavaScript code you need to enter into the Violentmonkey script editor:

// ==UserScript==
// @name         Telegram Pocket
// @namespace    https://jlelse.blog/dev/telegram-link-saving
// @version      0.1
// @description  Send current page link and title with a Telegram bot to a channel
// @author       Jan-Lukas Else
// @match        *://*/*
// @grant        GM_registerMenuCommand
// @grant        GM_xmlhttpRequest
// ==/UserScript==

(function() {
    'use strict';

    function sendTelegramMessage() {
      // Replace these values
      const botApiToken = 'YOUR_BOT_TOKEN';
      const chatId = -100YOUR_CHAT_ID;

      // Get the current page's URL and title
      const pageUrl = window.location.href;
      const pageTitle = document.title;

      // Telegram API endpoint for sending messages
      const telegramApiUrl = `https://api.telegram.org/bot${botApiToken}/sendMessage`;

      // Construct the message
      const message = `${pageTitle}\n\n${pageUrl}`;

      // Send the message to the Telegram Bot
      GM_xmlhttpRequest({
          method: 'POST',
          url: telegramApiUrl,
          data: JSON.stringify({
              chat_id: chatId, // Replace with your Telegram chat ID
              text: message
          }),
          headers: {
              'Content-Type': 'application/json'
          },
          onload: function(response) {
              console.log('Message sent to Telegram Bot:', response.responseText);
          },
          onerror: function(error) {
              console.error('Error sending message:', error);
          }
      });
    }

    // Register the manual activation menu command
    GM_registerMenuCommand('Save to Telegram Links', sendTelegramMessage);

})();

Remember to replace the YOUR_BOT_TOKEN and YOUR_CHAT_ID placeholders with the appropriate values for your setup.

Update: Originally, I tried to use a bookmarklet and shared the code here on this blog post. However a userscript is the better method as a lot of sites use CSP restrictions (for good reasons) and a userscript manager like Violentmonkey allows sending HTTP requests even from those sites.

Jan-Lukas Else
Interactions & Comments