This repository has been archived by the owner on Dec 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
334 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# React + Vite |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters