DEV Community

Cover image for How I made Telegram to Notion Bot
FranP-code
FranP-code

Posted on • Edited on • Originally published at open-react-blog.netlify.app

How I made Telegram to Notion Bot

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.

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 (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, 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.

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()
})
Enter fullscreen mode Exit fullscreen mode

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 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 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.

8. Publish the final version

Once the website has been “stunningly” developed, I have to update it on Reddit and publish it on Hacker News, 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 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 is the link to the Bot itself

And here is the link to the repository on Github

demo

Thank you very much and a hug! 🙌

Top comments (1)

Collapse
 
theresaspry profile image
TheresaSpry

How can I make my first Telegram bot? wazifa for love