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

Fastify example #29

Closed
KnorpelSenf opened this issue Sep 12, 2023 · 11 comments · Fixed by #30
Closed

Fastify example #29

KnorpelSenf opened this issue Sep 12, 2023 · 11 comments · Fixed by #30

Comments

@KnorpelSenf
Copy link
Member

Can you please add an example using webhook with secret_token, ngrok and fastify? I spent too much time trying to get it to work.

Try to use express v4.18.2, it didn't handle errors..

Originally posted by @gomez-git in grammyjs/grammY#486 (comment)

@KnorpelSenf
Copy link
Member Author

@gomez-git could you take a look at #30 and leave an approval there if you could make it work?

@gomez-git
Copy link

gomez-git commented Sep 13, 2023

@gomez-git could you take a look at #30 and leave an approval there if you could make it work?

I think about this one

import { Bot, webhookCallback } from 'grammy';
import fastify from 'fastify';
import ngrok from '@ngrok/ngrok';

const { BOT_TOKEN, PORT, SECRET_TOKEN } = process.env;

const bot = new Bot(BOT_TOKEN);

bot.on('message:text', (ctx) => ctx.reply(ctx.message.text));

const server = fastify();

server.post('/webhook', webhookCallback(bot, 'fastify', { secretToken: SECRET_TOKEN })); 

server.listen({ port: +PORT }, async (err, address) => {
  if (err) {
    console.error(err);
    process.exit(1);
  }
  const url = await ngrok.connect(+PORT);
  await bot.api.setWebhook(`${url}/webhook`, { secret_token: SECRET_TOKEN });
});

@KnorpelSenf
Copy link
Member Author

Perhaps we should use the official package https://www.npmjs.com/package/@ngrok/ngrok instead?

@gomez-git
Copy link

Perhaps we should use the official package https://www.npmjs.com/package/@ngrok/ngrok instead?

I use it in my snippet, isn't it?

@KnorpelSenf
Copy link
Member Author

@rojvv
Copy link
Member

rojvv commented Sep 14, 2023

@KnorpelSenf They are importing @ngrok/ngrok in #29 (comment), 3rd line.

@KnorpelSenf
Copy link
Member Author

Oh what

@KnorpelSenf
Copy link
Member Author

I'm blind

@KnorpelSenf
Copy link
Member Author

Yes, let's do that. @AbbassAlmusawi could you update your code in #30 with the above package? I'll merge after that.

@gomez-git
Copy link

gomez-git commented Sep 14, 2023

Dockerfile

FROM node:18-alpine

WORKDIR /usr/src

COPY ./package*.json ./

EXPOSE 443

RUN npm ci

COPY . ./

CMD ["ts-node", "fastify-ngrok-webhook.ts"]

.dockerignore

node_modules

command

docker build --tag grammy_fastify_example .
docker run --detach \
  --name grammy_fe \
  -e BOT_TOKEN='' \
  -e PORT='' \
  -e SECRET_TOKEN='' \
  grammy_fastify_example

@AbbassAlmusawi
Copy link
Contributor

Done, please see #30 and tell me if there are any mistakes of suggestions

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants