How I made Telegram to Notion Bot
Tags: telegram, notion, javascript, node
Published: 2022-05-09 22:34:51

Hello, good morning everyone, this is my first post on Dev.To

I hope it will be useful to different developers to carry out projects related to Bots in Telegram as well as any project in general.

I will try to make this post as complete as possible, any questions or suggestions do not hesitate to comment.

## 1. Select the Framework on which to work with Telegram

For a long time I wanted to carry out this small project, around 6 months, and unfortunately I "married" the only framework I knew, Telegraf.

Today, it is outdated and has problems with states, a way to assign variables to a specific user, useful to alter the behavior during the execution and use of the bot.

For this reason, and thanks to a recommendation launched by the NPM package that unfortunately I don't know what it is, I discovered the wonderful Framework [grammY](https://grammy.dev/).

### Important points to choose it

1. **Sessions work** thanks to the Sessions and Storing Data plugin
2. It has a vast and great documentation.
    Maybe it could be better organized, but it's just a detail.
3. It is "backward compatible" with the code in Telegraf.
    In a surprisingly easy way, I migrated my app from Telegraf to grammY. It is not the way demonstrated by them to write an app, but, it worked.
    

## 2. Register the Bot on Telegram

This is very simple, accessing the official account of [@BotFather](http://t.me/BotFather) (from Telegram obviously), you can register a bot and customize it with your own commands.

The most important ones, `/newbot` to create one and `/mybots` to manage them.

## 3. Make Bot Website

It is necessary for the next steps to create a website **with a defined backend**, that is, with Node.js, PHP or another backend language that allows queries from the server and not from the client.

It was a huge frustration when I first tried this project and found out that a React site wasn't enough.

This must have endpoints for the **privacy policy** and the **terms of use**.

## 4. Register the public integration in Notion

By accessing [Notion's My Integrations](https://www.notion.so/my-integrations), you can register an integration, initially private, and then make it public.

The latter necessarily requires:

- **Redirect URIs**, link to where the user will be redirected after authorizing your integration with their pages or databases in Notion
- **Company name**, or your name in case a company is not working behind
- **Tagline**, small description
- **Privacy policy**, link to the privacy policies that you previously defined
- **Terms of use**, link to the terms of use that you previously defined
- **Email support**, with an email to support the client of your integration

## 5. Write the operation of the different bot commands

This is the most personal part of all, which will vary from project to project, defining the functionality of the bot.

For this, first of all, I wrote a middleware to check if the bot was in development or production mode. With this, only the user with a certain ID could access the bot's functionalities.

```jsx
bot.use(async (ctx, next) => {
    if (process.env.NODE_ENV !== "development") {
        await next()
        return
    }

    if (ctx.from.id !== parseInt(process.env.MY_USER_ID)) {
        ctx.reply('⚠️ Sorry, this bot is on development for now... \nStay alert >
        return
    }

    next()
})
```

Once this was done, I did the development command by command:

- **Start**, where I welcome the bot
- **On message** in the case in which the bot is in development and the account that has accessed is not mine.
- **Auth**, where a state is created saying if an auth code is expected from the user and a link is provided to go to a server to generate the auth code (details later).
- **Help**, where the [Github repository](https://github.com/FranP-code/Telegram-to-Notion-Bot) of this bot is shown
- **Roadmap**, to return the roadmap of my project
- **Announcement**, to be able to make global announcements for each user that will interact with the project

Additionally I made the necessary **Middlewares** (functions that are executed before the bot functions) to check if:

- The user wanted to authorize the bot, therefore, intervene the message to verify and save his auth code
- The user wanted to make an announcement, adding some layers of security in the middle

And some more features, such as detecting when the user taps on one of the buttons that are returned with the Notion databases, among others.

## 6. Send it to production

Using the [railway](https://railway.app/) service, I deployed it to production without much difficulty.

It is noteworthy that every time the bot broke, which happened continuously, the service very quickly notified it by email.

It has a *freemium* model and has a limit of monthly CPU and RAM usage. From there, it is paid.

## 7. Publish the beta version

I posted on the Notion subreddit the highlights of my project, initially posting the [bot with beta website](https://www.reddit.com/r/Notion/comments/u24hu9/telegram_to_notion_bot/).

## 8. Publish the final version

Once the website has been “stunningly” developed, [I have to update it on Reddit](https://www.reddit.com/r/Notion/comments/udabqw/some_updates_and_one_more_demo_of_my_last_project/) and publish it on [Hacker News](https://news.ycombinator.com/item?id=31216997), where I got the most traffic to the bot.

## 9. Design the roadmap

Seeing user feedback on Reddit, I decided to make a roadmap to allow users to track planned features and their progress.

It was advertised within the bot and is available as one of its commands.

## 10. Post it to Dev.to

Once the vast majority of the work is finished, and I still have to develop additional features, I have to make a post to show it with the great [Dev.to](http://dev.to/) community, a site I have frequently visited but where I had never interacted.

I hope you liked this first post.

# More information about the Bot

**Features**

- **Free**
- **Open source**
- **Unlimited**
- **Forever** (at least while I can afford the host of it)

**[Here](https://t.me/OpenTelegramToNotionBot)** is the link to the Bot itself

**[And here](https://github.com/FranP-code/Open-Telegram-to-Notion-Bot)** is the link to the repository on Github

![demo](https://i.ibb.co/pJ10w9j/Telegram-To-Notion-Bot-Demo-Acelerated.gif)

Thank you very much and a hug! 🙌