Skip to main content

Warning: Could be unstable, I donā€™t know.

GramJS for Deno

GramJS is a popular Telegram client written in JavaScript for Node.js and browsers, with its core being based on Telethon.

And this, hehe. This is a port of GramJS for Deno. It is written in TypeScript.

What is Deno? https://deno.land

Documentation

Consider following the original GramJS documentation. However, you can follow the Getting Started example for a start.

Getting started

Note: This was copied from GramJS README, then modified.

Here youā€™ll learn how to obtain necessary information to create telegram application, authorize into your account and send yourself a message.

First, youā€™ll need to obtain an API ID and hash:

  1. Login into your Telegram account
  2. Then click ā€œAPI development toolsā€ and fill your application details (only app title and short name required)
  3. Finally, click ā€œCreate applicationā€

Warning

Never share any API/authorization details, that will compromise your application and account.

When youā€™ve successfully created the application set the API ID and hash you just got from Telegram in the following code.

import { StringSession, TelegramClient } from "https://deno.land/x/gram/mod.ts";

const apiId = 123456;
const apiHash = "abcd1234";
// fill this later with the value from session.save()
const stringSession = new StringSession("");

(async function () {
  console.log("Loading interactive example...");
  const client = new TelegramClient(stringSession, apiId, apiHash, {
    connectionRetries: 5,
  });

  await client.start({
    phoneNumber: () => prompt("Enter your phone number:")!,
    password: async () => await prompt("Enter your password:")!,
    phoneCode: async () => await prompt("Enter the code you received:")!,
    onError: (err) => console.log(err),
  });

  console.log("You should now be connected.");
  // Save this string to avoid logging in again
  console.log(client.session.save());

  // Send a message to yourself
  await client.sendMessage("me", { message: "Hello!" });
})();

Lets run it:

deno run -A example-file.ts

Youā€™ll be prompted to enter your phone number, and code you received. Then you should have a ā€œHelloā€ message in your Saved Messages.

Notes

This is a direct port of GramJS for Deno. And this is just an attempt, which turns out to be an successful attempt. At least, the startup and the getting started example works. Iā€™m glad about it. And I donā€™t know this works for you, but it works for me :)

It took me like 4 days. Total of 20h6m for this repository alone. And including ported dependencies and figuring out the original code, its a total of almost 34.8h. I didnā€™t just copy and paste stuff ā€” I did, but I manually wrote lot of the files.

I havenā€™t added any JSDocs yet. Iā€™ll add them later. I didnā€™t add any for ease of porting. Its a mess if there are a lot of comments while porting (We can just collapse them, I know. But my machine canā€™t take too much load).

I had to port the following Node packages to Deno. I know that some of them is not even has to be ported, but I didnā€™t realised that then.

Contributing

Pull requests are welcome. But, I only suggest you to open pull requests if they are related to fixes or improvments with the porting, migrating from node packages to Denoā€™s built-in or std stuff, etc. Pull requests related to GramJS core itself are not very welcome here, because I was thinking to keep up with the original repository.

I am not very experienced in most of the core stuff used in GramJS. Most importantly, websockets. I really want to migrate from using node packages socks and websocket to Denoā€™s built-in websocket stuff, if it is possible (And it should be possible). If you can help migrating it, it would be great!