Skip to content

Migrating to ESM #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: nightly
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
15.2.0
18.6.0
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,13 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
<!-- prettier-ignore-start -->
<!-- markdownlint-disable -->
<table>
<tr>
<td align="center"><a href="https://ptk.dev"><img src="https://avatars1.githubusercontent.com/u/442844?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Patryk Rzucidło</b></sub></a><br /><a href="https://github.com/ptkdev/ptkdev-boilerplate/node-telegram-bot-boilerplate/commits?author=ptkdev" title="Code">💻</a> <a href="#translation-ptkdev" title="Translation">🌍</a> <a href="https://github.com/ptkdev/ptkdev-boilerplate/node-telegram-bot-boilerplate/commits?author=ptkdev" title="Documentation">📖</a> <a href="https://github.com/ptkdev/ptkdev-boilerplate/node-telegram-bot-boilerplate/issues?q=author%3Aptkdev" title="Bug reports">🐛</a></td>
<td align="center"><a href="https://edge33.github.io"><img src="https://avatars1.githubusercontent.com/u/5662280?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Francesco Maida</b></sub></a><br /><a href="https://github.com/ptkdev/ptkdev-boilerplate/node-telegram-bot-boilerplate/commits?author=edge33" title="Code">💻</a> <a href="https://github.com/ptkdev/ptkdev-boilerplate/node-telegram-bot-boilerplate/commits?author=edge33" title="Documentation">📖</a></td>
<td align="center"><a href="https://verhyppo.github.io"><img src="https://avatars.githubusercontent.com/u/3539384?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Veronica Di Giorgio</b></sub></a><br /><a href="#infra-verhyppo" title="Infrastructure (Hosting, Build-Tools, etc)">🚇</a> <a href="https://github.com/ptkdev/ptkdev-boilerplate/node-telegram-bot-boilerplate/commits?author=verhyppo" title="Documentation">📖</a></td>
</tr>
<tbody>
<tr>
<td align="center" valign="top" width="16.66%"><a href="https://ptk.dev"><img src="https://avatars1.githubusercontent.com/u/442844?v=4?s=100" width="100px;" alt="Patryk Rzucidło"/><br /><sub><b>Patryk Rzucidło</b></sub></a><br /><a href="https://github.com/ptkdev/ptkdev-boilerplate/node-telegram-bot-boilerplate/commits?author=ptkdev" title="Code">💻</a> <a href="#translation-ptkdev" title="Translation">🌍</a> <a href="https://github.com/ptkdev/ptkdev-boilerplate/node-telegram-bot-boilerplate/commits?author=ptkdev" title="Documentation">📖</a> <a href="https://github.com/ptkdev/ptkdev-boilerplate/node-telegram-bot-boilerplate/issues?q=author%3Aptkdev" title="Bug reports">🐛</a></td>
<td align="center" valign="top" width="16.66%"><a href="https://edge33.github.io"><img src="https://avatars1.githubusercontent.com/u/5662280?v=4?s=100" width="100px;" alt="Francesco Maida"/><br /><sub><b>Francesco Maida</b></sub></a><br /><a href="https://github.com/ptkdev/ptkdev-boilerplate/node-telegram-bot-boilerplate/commits?author=edge33" title="Code">💻</a> <a href="https://github.com/ptkdev/ptkdev-boilerplate/node-telegram-bot-boilerplate/commits?author=edge33" title="Documentation">📖</a></td>
<td align="center" valign="top" width="16.66%"><a href="https://verhyppo.github.io"><img src="https://avatars.githubusercontent.com/u/3539384?v=4?s=100" width="100px;" alt="Veronica Di Giorgio"/><br /><sub><b>Veronica Di Giorgio</b></sub></a><br /><a href="#infra-verhyppo" title="Infrastructure (Hosting, Build-Tools, etc)">🚇</a> <a href="https://github.com/ptkdev/ptkdev-boilerplate/node-telegram-bot-boilerplate/commits?author=verhyppo" title="Documentation">📖</a></td>
</tr>
</tbody>
</table>

<!-- markdownlint-restore -->
Expand Down
4 changes: 2 additions & 2 deletions app/configs/config.js.tpl → app/configs/config.ts.tpl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = {
export default {
// Server
"server": {
"port": 5000,
Expand All @@ -9,7 +9,7 @@ module.exports = {
token: "BOT_TOKEN",
},

mode: "poll", // or webhook
mode: "poll", // poll | webhook | localtunnel
webhook: {
url: "https://sample.host.com:8443",
port: 8443,
Expand Down
6 changes: 3 additions & 3 deletions app/functions/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
*/
import bot from "@app/functions/telegraf";
import * as databases from "@app/functions/databases";
import config from "@configs/config";
import { launchPolling, launchWebhook } from "./launcher";
import config from "@configs/config";

/**
* command: /quit
Expand Down Expand Up @@ -59,8 +59,8 @@ const start = async (): Promise<void> => {
*/
const launch = async (): Promise<void> => {
const mode = config.mode;
if (mode === "webhook") {
launchWebhook();
if (["webhook", "localtunnel"].includes(mode)) {
launchWebhook(mode);
} else {
launchPolling();
}
Expand Down
28 changes: 18 additions & 10 deletions app/functions/databases.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@
*/
import type { TelegramUserInterface } from "@app/types/databases.type";
import configs from "@configs/config";
import lowdb from "lowdb";
import lowdbFileSync from "lowdb/adapters/FileSync";
import { JSONFileSync, LowSync } from "lowdb";

const databases = { users: lowdb(new lowdbFileSync<{ users: TelegramUserInterface[] }>(configs.databases.users)) };
const adapter = new JSONFileSync<{ users: TelegramUserInterface[] }>(configs.databases.users);

databases.users = lowdb(new lowdbFileSync(configs.databases.users));
databases.users.defaults({ users: [] }).write();
const databases = { users: new LowSync(adapter) };

databases.users.data = { users: [] };
databases.users.write();

/**
* writeUser()
Expand All @@ -30,13 +31,20 @@ databases.users.defaults({ users: [] }).write();
*
*/
const writeUser = async (json: TelegramUserInterface): Promise<void> => {
const user_id = databases.users.get("users").find({ id: json.id }).value();

if (user_id) {
databases.users.get("users").find({ id: user_id.id }).assign(json).write();
databases.users.read();
databases.users.data?.users.find((u) => u.id === json.id);
if (!databases.users.data) {
throw new Error("Users database not found");
}
const user_id = databases.users.data.users.findIndex((u) => u.id === json.id);
let newUsers = databases.users.data.users ? [...databases.users.data.users] : [];
if (user_id !== -1) {
newUsers[user_id] = { ...json };
} else {
databases.users.get("users").push(json).write();
newUsers = [...newUsers, { ...json }];
}
databases.users.data.users = newUsers;
databases.users.write();
};

export { databases, writeUser };
Expand Down
34 changes: 17 additions & 17 deletions app/functions/launcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,38 +10,37 @@
import bot from "./telegraf";
import config from "@configs/config";
import fs from "fs";
import localtunnel from "localtunnel";
import { Input } from "telegraf";

const launchPolling = (): void => {
bot.launch();
};

const launchSelfSigned = async (webhook_url: string, secret_path: string) => {
const { port } = config.webhook;
const launchSelfSigned = async () => {
const { port, url } = config.webhook;
const path = `${process.cwd()}/certs`;
const cert = fs.readFileSync(`${path}/PUBLIC.pem`);
const pk = fs.readFileSync(`${path}/PK.key`);
const tls_options = {
key: pk,
cert: cert,
};
const certFile = Input.fromLocalFile(`${path}/PUBLIC.pem`);

await bot.launch({
webhook: {
tlsOptions: tls_options,
hookPath: secret_path,
domain: url,
port: port,
},
});
bot.telegram.setWebhook(`${webhook_url}${secret_path}`, {
certificate: {
source: cert,
certificate: certFile,
tlsOptions: tls_options,
},
});
};

const launchLocalTunnel = async (secret_path: string, port: number) => {
const localtunnel = (await import("localtunnel")).default;
const tunnel = await localtunnel({ port });
bot.launch({
await bot.launch({
webhook: {
domain: tunnel.url,
hookPath: secret_path,
Expand All @@ -50,22 +49,23 @@ const launchLocalTunnel = async (secret_path: string, port: number) => {
});
};

const launchWebhook = async (): Promise<void> => {
const launchWebhook = async (mode: string): Promise<void> => {
const { port, url, self_signed } = config.webhook;
const secret_path = `/telegraf/${bot.secretPathComponent()}`;

// Set telegram webhook
// this runs localtunnel to develop the bot on localhost
// acts as a reverse proxy for telegrm calls to our websocket
const webhook_url = url;
if (config.debug) {
if (mode === "localtunnel") {
return launchLocalTunnel(secret_path, port);
} else if (self_signed) {
return launchSelfSigned(webhook_url, secret_path);
}

if (mode === "webhook" && self_signed) {
return launchSelfSigned();
} else {
return bot.launch({
webhook: {
domain: webhook_url,
domain: url,
hookPath: secret_path,
port: port,
},
Expand Down
8 changes: 3 additions & 5 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
module.exports = {
export default {
preset: "ts-jest",
testEnvironment: "node",
globals: {
"ts-jest": {
tsconfig: "tsconfig.json",
},
transform: {
"^.+\\.ts?$": ["ts-jest", { tsconfig: "tsconfig.json" }],
},
};
Loading