Skip to content
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

Add Vercel hosting guide #543

Merged
merged 22 commits into from
Nov 21, 2022
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
c2698bd
feat: add Vercel hosting guide
jgcarrillo Nov 19, 2022
46307b0
Apply suggestions from code review
jgcarrillo Nov 19, 2022
ffb11df
Apply suggestions from code review
jgcarrillo Nov 19, 2022
6480a1b
fix: add env variable to bot token for a better approach
jgcarrillo Nov 20, 2022
c78c92a
Apply suggestions from code review
jgcarrillo Nov 20, 2022
1c13819
Apply suggestions from code review
jgcarrillo Nov 20, 2022
e0d9bec
Apply suggestions from code review
jgcarrillo Nov 20, 2022
cd6c441
Apply suggestions from code review
jgcarrillo Nov 21, 2022
e13e7cb
Apply suggestions from code review
jgcarrillo Nov 21, 2022
5874916
Update site/docs/hosting/vercel.md
Nov 21, 2022
28d57f0
Update site/docs/hosting/vercel.md
Nov 21, 2022
4663749
Apply suggestions from code review
jgcarrillo Nov 21, 2022
f320e7c
Apply suggestions from code review
jgcarrillo Nov 21, 2022
d76b020
Apply suggestions from code review
jgcarrillo Nov 21, 2022
0a5b9c7
fix: change dropdown order and add Vercel env variables reminder
jgcarrillo Nov 21, 2022
8f8f2eb
Merge incoming changes with local ones
jgcarrillo Nov 21, 2022
dc5b61c
fix: apply correct format
jgcarrillo Nov 21, 2022
32e7259
Apply suggestions from code review
jgcarrillo Nov 21, 2022
f5ec74a
Apply suggestions from code review
jgcarrillo Nov 21, 2022
c82fbcd
fix: remove info about delete/check webhook
jgcarrillo Nov 21, 2022
656028c
Merge incoming changes with local ones
jgcarrillo Nov 21, 2022
015eaf0
Apply suggestions from code review
jgcarrillo Nov 21, 2022
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
4 changes: 4 additions & 0 deletions site/docs/.vuepress/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,10 @@ export default defineUserConfig({
text: "Virtual Private Server",
link: "/hosting/vps.html",
},
{
jgcarrillo marked this conversation as resolved.
Show resolved Hide resolved
text: "Vercel",
link: "/hosting/vercel.html",
},
],
},
],
Expand Down
118 changes: 118 additions & 0 deletions site/docs/hosting/vercel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
# Hosting: Vercel Serverless Functions

This tutorial will guide you on how to deploy your bot to [Vercel](https://vercel.com/) by using [Vercel Serverless Functions](https://vercel.com/docs/concepts/functions/serverless-functions), assuming that you already have a [Vercel](https://vercel.com) account.

## Project Structure

The only prerequisite to get started with **Vercel Serverless Functions** is to move your code to the `api/` directory as shown below.
You can also see [Vercel's documentation](https://vercel.com/docs/concepts/functions/serverless-functions#deploying-serverless-functions) for more on this.

```asciiart:no-line-numbers
.
├── node_modules/
├── build/
├── api/
│ └── bot.ts
├── package.json
├── package-lock.json
└── tsconfig.json
```

If you are using TypeScript, you might as well want to install `@vercel/node` as a dev dependency, but it is not mandatory to follow this guide.
jgcarrillo marked this conversation as resolved.
Show resolved Hide resolved

## Configuring Vercel

The next step is to create a `vercel.json` file at the top level of your project.
For our example structure, its content would look be:
jgcarrillo marked this conversation as resolved.
Show resolved Hide resolved

```json
{
"functions": {
"api/bot.ts": {
"memory": 1024,
"maxDuration": 10
}
}
}
```

> Vercel's free subscription has restrictions on quotas, which we need to enable in the configuration file, `vercel.json` ([grammY Examples](https://github.com/grammyjs/examples/blob/main/vercel-bot/api/index.ts)).
jgcarrillo marked this conversation as resolved.
Show resolved Hide resolved

If you want to learn more about this configuration file, see [its documentation](https://vercel.com/docs/project-configuration).
jgcarrillo marked this conversation as resolved.
Show resolved Hide resolved

## Configuring TypeScript

In our `tsconfig.json`, we have to specify our output directory as `build/`, and our root directory as `api/`.
This is important since we will specify them in Vercel's deploy options.

```json{5,8}
{
"compilerOptions": {
"target": "ES2019",
"module": "commonjs",
"rootDir": "./api",
"moduleResolution": "node",
"resolveJsonModule": true,
"outDir": "./build",
"allowSyntheticDefaultImports": true,
"esModuleInterop": true,
"strict": true,
"skipLibCheck": true
}
}
```

## The Main File

Regardless of using TypeScript or JavaScript, we should have a source file through which our bot runs.
It should look roughly like this:

```ts
import { Bot, webhookCallback } from "grammy";

const bot = new Bot(process.env.BOT_TOKEN); // <-- insert your bot token from environment variable
jgcarrillo marked this conversation as resolved.
Show resolved Hide resolved
jgcarrillo marked this conversation as resolved.
Show resolved Hide resolved

export default webhookCallback(bot, "http"); // <-- This line will do the trick!
jgcarrillo marked this conversation as resolved.
Show resolved Hide resolved
```

## Go to Vercel Website
jgcarrillo marked this conversation as resolved.
Show resolved Hide resolved

Assuming you have a Vercel account and have your GitHub account connected to Vercel, add a new project and select your GitHub bot repository.
jgcarrillo marked this conversation as resolved.
Show resolved Hide resolved
In the _Build & Development Settings_:
jgcarrillo marked this conversation as resolved.
Show resolved Hide resolved

- Output directory: `build`
- Install command: `npm install`

Don't forget to add **environment variables** such as your **bot token**.
Once you have done, deploy it!

## Setting Up the Telegram Webhook
jgcarrillo marked this conversation as resolved.
Show resolved Hide resolved

Your bot is ready.
jgcarrillo marked this conversation as resolved.
Show resolved Hide resolved
The last step is to add the webhook that connects your deployment with Telegram and your bot token.
jgcarrillo marked this conversation as resolved.
Show resolved Hide resolved
Here is the URL you need to visit using your browser:
jgcarrillo marked this conversation as resolved.
Show resolved Hide resolved

```md:no-line-numbers
https://api.telegram.org/bot<BOT_TOKEN>/setWebhook?url=<HOST_URL>
```

The `HOST_URL` is a little tricky, because you need to use your **Vercel app domain following with the route to the bot code**, for example `https://appname.vercel.app/api/bot`.
Where `bot` is your `bot.ts` or `bot.js` file.
jgcarrillo marked this conversation as resolved.
Show resolved Hide resolved

If everything was successful, you should now see this in your browser window.
jgcarrillo marked this conversation as resolved.
Show resolved Hide resolved

```json
{
"ok": true,
"result": true,
"description": "Webhook was set"
}
```

Other useful commands are:

- Info about webhook: `https://api.telegram.org/bot{BOT_TOKEN}/getWebhookInfo`
- Delete webhook: `https://api.telegram.org/bot{BOT_TOKEN}/deleteWebhook`
Copy link
Member

@rojvv rojvv Nov 21, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that this is

Copy link
Author

@jgcarrillo jgcarrillo Nov 21, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I delete it?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it would be way better.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As soon as this PQ is done and merged, we should probably copy this paragraph to the other guides, as well. Not very DRY, but very useful indeed.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!


Congratulations!
Your bot should now be up and running.