Skip to content
This repository has been archived by the owner on Dec 26, 2023. It is now read-only.

Commit

Permalink
Write some Hello World
Browse files Browse the repository at this point in the history
  • Loading branch information
rojvv committed Aug 31, 2023
1 parent 393a5df commit f257cf4
Show file tree
Hide file tree
Showing 8 changed files with 334 additions and 3 deletions.
7 changes: 5 additions & 2 deletions src/SUMMARY.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Summary

- [Introduction](./introduction.md)

<!-- - [Getting Started](./getting-started.md) -->
- [Hello World](./hello-world.md)
- [Deno](./hello-world/deno.md)
- [Node.js](./hello-world/node-js.md)
- [React + Vite](./hello-world/react-vite.md)
- [Vanilla JavaScript](./hello-world/vanilla-javascript.md)
1 change: 0 additions & 1 deletion src/getting-started.md

This file was deleted.

9 changes: 9 additions & 0 deletions src/hello-world.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Hello World

Here you will learn how you can add MTKruto to your project and invoke a ping
request. Choose your preferred environment to get started.

- [Deno](./hello-world/deno.md)
- [Node.js](./hello-world/node-js.md)
- [React + Vite](./hello-world/react-vite.md)
- [Vanilla JavaScript](./hello-world/vanilla-javascript.md)
99 changes: 99 additions & 0 deletions src/hello-world/deno.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
# Deno

MTKruto is available for Deno at <https://deno.land/x/mtkruto>. Here’s how you
can initialize a client, connect it, and invoke a ping request.

## Prerequisites

- [Deno](https://deno.land)

## Initial Setup

For this guide, we will have only a single file and we won’t persist any data
since we won’t reuse the connection. For most projects, we recommend having a
directory with a Deno configuration file.

Create a file called `ping.ts` and import the `Client` class from MTKruto:

```ts
import { Client } from "https://deno.land/x/mtkruto/mod.ts";
```

> Remember to replace the import specifier to explicitly specify a specific
> version of MTKruto. We have omitted it here to stay simple.
## Starting the Client

Before we can invoke functions, we first initialize and start a client.

Starting the client for the first time will take a little amount of time
regardless of the connection speed. It might even take more if you are
connecting to a test server. In this guide, our client will always take a little
to start since we don’t have a persistency layer for the sake of staying simple.

```ts
const client = new Client();

console.log("Starting client...");
await client.start();
console.log("Client started.");
```

The above code initializes a client with the default parameters and no API
credentials. After that, it initiates a connection with Telegram’s test servers.

## Invoking Functions

You can make calls directly to the Telegram API using the `invoke()` method of
the client. The argument you pass to it should be an instance of a call
definition from the `functions` namespace.

Update your import declaration to look like this:

```ts
import { Client, functions } from "https://deno.land/x/mtkruto/mod.ts";
```

And invoke a `ping` call:

```ts
const before = Date.now();
await client.invoke(new functions.Ping({ pingId: 1n }));
const diff = Math.floor(Date.now() - before);
console.log("Ping took", `${diff}ms.`);
```

## Conclusion

Your final code should look like this:

File name: ping.ts

```ts
import { Client, functions } from "https://deno.land/x/mtkruto/mod.ts";

const client = new Client();

console.log("Starting client...");
await client.start();
console.log("Client started.");

const before = Date.now();
await client.invoke(new functions.Ping({ pingId: 1n }));
const diff = Math.floor(Date.now() - before);
console.log("Ping took", `${diff}ms.`);
```

Use the following command to run it:

```bash
deno run --allow-net ping.ts
```

You should get a result like this:

```ts
Starting client...
Client started.
Ping took 215ms.
```
108 changes: 108 additions & 0 deletions src/hello-world/node-js.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
# Node.js

MTKruto is available for Node.js on npm as
[`@mtkruto/node`](https://npm.im/@mtkruto/node). Here you will learn how you can
use MTKruto in your Node.js project.

## Prerequisites

- [Node.js](https://nodejs.org)

## Initial Setup

First, create a directory for our project, initialize package.json, and install
MTKruto:

```ts
mkdir ping
cd ping
npm init -y
npm install @mtkruto/node
```

> We’re using npm here, but you can also use pnpm, yarn, or anything else you
> prefer.
Create a file called `ping.js` and import the `Client` class from MTKruto:

```ts
const { Client } = require("@mtkruto/node");
```

> We’re using the CJS syntax here, but ESM is also supported by `@mtkruto/node`.
## Starting the Client

Before we can invoke functions, we first initialize and start a client.

Starting the client for the first time will take a little amount of time
regardless of the connection speed. It might even take more if you are
connecting to a test server. In this guide, our client will always take a little
to start since we don’t have a persistency layer for the sake of staying simple.

```ts
const client = new Client();

console.log("Starting client...");
await client.start();
console.log("Client started.");
```

The above code initializes a client with the default parameters and no API
credentials. After that, it initiates a connection with Telegram’s test servers.

## Invoking Functions

You can make calls directly to the Telegram API using the `invoke()` method of
the client. The argument you pass to it should be an instance of a call
definition from the `functions` namespace.

Update your import declaration to look like this:

```ts
const { Client, functions } = require("@mtkruto/node");
```

And invoke a `ping` call:

```ts
const before = Date.now();
await client.invoke(new functions.Ping({ pingId: 1n }));
const diff = Math.floor(Date.now() - before);
console.log("Ping took", `${diff}ms.`);
```

## Conclusion

Your final code should look like this:

File name: ping.js

```ts
const { Client, functions } = require("@mtkruto/node");

const client = new Client();

console.log("Starting client...");
await client.start();
console.log("Client started.");

const before = Date.now();
await client.invoke(new functions.Ping({ pingId: 1n }));
const diff = Math.floor(Date.now() - before);
console.log("Ping took", `${diff}ms.`);
```

Use the following command to run it:

```bash
node ping.js
```

You should get a result like this:

```ts
Starting client...
Client started.
Ping took 215ms.
```
1 change: 1 addition & 0 deletions src/hello-world/react-vite.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# React + Vite
110 changes: 110 additions & 0 deletions src/hello-world/vanilla-javascript.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
# Vanilla JavaScript

It is less likely that you will use MTKruto this way. This guide is mainly to
demonstrate how simple it is to get started with MTKruto.

## Prequisites

- A modern web browser
- A text editor

## Setup

Create a file called `ping.html` with the following contents:

```html
<!DOCTYPE html>
<html>
<head>
<script type="module" defer>
import { Client } from "https://esm.sh/@mtkruto/browser";
</script>
</head>
<body>
</body>
</html>
```

## Starting the Client

Before we can invoke functions, we first initialize and start a client.

Starting the client for the first time will take a little amount of time
regardless of the connection speed. It might even take more if you are
connecting to a test server. In this guide, our client will always take a little
to start since we don’t have a persistency layer for the sake of staying simple.

Extend the contents of the `<script>` tag with this snippet:

```ts
const client = new Client();

console.log("Starting client...");
await client.start();
console.log("Client started.");
```

The above code initializes a client with the default parameters and no API
credentials. After that, it initiates a connection with Telegram’s test servers.

## Invoking Functions

You can make calls directly to the Telegram API using the `invoke()` method of
the client. The argument you pass to it should be an instance of a call
definition from the `functions` namespace.

Update your import declaration to look like this:

```ts
import { Client, functions } from "https://esm.sh/@mtkruto/browser";
```

And invoke a `ping` call:

```ts
const before = Date.now();
await client.invoke(new functions.Ping({ pingId: 1n }));
const diff = Math.floor(Date.now() - before);
console.log("Ping took", `${diff}ms.`);
```

## Conclusion

Your final code should look like this:

File name: ping.html

```html
<!DOCTYPE html>
<html>
<head>
<script type="module" defer>
import { Client, functions } from "https://esm.sh/@mtkruto/browser";
const client = new Client();
console.log("Starting client...");
await client.start();
console.log("Client started.");
const before = Date.now();
await client.invoke(new functions.Ping({ pingId: 1n }));
const diff = Math.floor(Date.now() - before);
console.log("Ping took", `${diff}ms.`);
</script>
</head>
<body>
</body>
</html>
```

To run it, simply open that file with your browser. After opening the file, open
the developer tools, and navigate to the tab called Console.

You should see something like this:

```ts
Starting client...
Client started.
Ping took 215ms.
```
2 changes: 2 additions & 0 deletions src/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,5 @@ API.

- You can’t interact with the Telegram API using webhooks. You have to maintain
a connection with Telegram servers.

<!-- TODO: compare with TDLib -->

0 comments on commit f257cf4

Please sign in to comment.