Coder Social home page Coder Social logo

examples's Introduction

grammY examples

This repository contains various bots and setups to help you get started with grammY.

  1. Contact @BotFather to create a bot and get its token.
  2. Clone the repository.
  3. Paste your bot’s token to the source files of the examples you would like to run (e.g. ./echo.ts).
  4. Run npm install from the root.
  5. Run the examples this way:
npm run bot <name>

# For example, to run the echo example:
npm run bot echo

Hello World (echo)

A simple bot that echoes the text messages it receives.

Send a Single Message (send-message)

Not a real bot, only an illustration of sending a single message once. Might be useful in scripts.

Nested Menus (menu)

Advanced menu example bot that lets you manage different dishes (Pasta, Pizza, Sushi, Entrecôte) via a nested menu structure. It shows how to build more complex menus when all the data is generated at runtime.

Menus with Media (menu-with-photo)

Similar to the nested menu example, but with photos.

Sessions (stats)

Full-blown example bot that counts messages in a chat and stores the statistics in session objects.

Lazy Sessions (stats-lazy)

Full-blown example bot that counts photos in a chat and stores the statistics using lazy sessions. The advantage of lazy sessions is that the storage is only queried whenever data is actually needed.

(Note that this bot stores data in-memory, it does not connect to a database. Therefore, using lazy sessions does not make much sense. This bot is only an illustratation on how to use them.)

Runner Example (runner)

Illustrates how to use the grammY runner to run large bots.

Fastify Example (fastify)

Simple example for using grammY with Fastify and Ngrok

Scaling Example (scaling)

NOTE: This example is not endorsement of any directory structure. See Awesome grammY for live examples instead.

A minimal example bot that demonstrates how various grammY objects can be orchestrated together, including custom context flavors, transformers, sessions with initial data, composers, and routers.

Example bot that can run on Deno Deploy.

Example bot that can run on Supabase Edge Functions.

Example bot using Node.js on Vercel Serverless Functions.

Example bot using Deno on Vercel Serverless Functions.

Example bot using Bun on Vercel Serverless Functions.

Example bot using Vercel Edge Functions.

Example bot that can run using AWS Lambda.

Example Deno bot that can run using Docker.

Example Node.js bot that can run using Docker.

Use Docker to setup a local Bot API server and run your bot on it.

Use Docker to setup a local Bot API server with a self-signed TLS certificate, and run your bot on it.

Please Contribute

We could have some more examples here, for example:

  • command handling
  • filter queries
  • inline keyboards
  • file support

and other things.

examples's People

Contributors

abbassalmusawi avatar aidanaden avatar dcdunkan avatar deptyped avatar flmel avatar habemuscode avatar knightniwrem avatar knorpelsenf avatar linbuxiao avatar loskir avatar matyifkbt avatar ponomarevlad avatar rojvv avatar roziscoding avatar thorwebdev avatar walidvb avatar waptik avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

examples's Issues

grammy bot only works in aws-lambda node18.x

I ran the example in setups/aws-lambda folder, I found that grammy bot only works in aws-lambda nodejs18.x, it doesn't work in 20.x. Is it because grammy only supports node 18?

Conversations. switching between questions. & input validation

Hi.
I have a quite popular task for which I haven't found any examples.

1. You need to switch between questions.

System:

question 1: How much?
user: 100
question 2: ure good?
user: Yes.
question 3: Want some apples?
user: back
question 2: ure good?
user: back
question 1: How much?

as you can see - the user switched back 2 questions and the bot asked them again. Of course, after the user answers, the bot will move on in the script and the user can also go back.

2. Input validation.

Criterion for user's answer to question 1: parseInt(ctx.message.text).
System:

question 1: How much?
user: asdjajsdja
question 1: How much?
user: asaasd
question 1: How much?
user: 100
question 2: ure good?

the bot did not accept the answer until it met the criterion.
Of course, this should work in conjunction with point 1 (switching between questions).

next-node webhook example fails in Vercel production environment

Description:

The webhook example provided at https://github.com/grammyjs/examples/tree/main/setups/next-node does not work correctly when deployed to Vercel. The following behaviour occurs:

  • Production Timeout: The webhook callback function appears to run indefinitely, eventually causing a timeout on Vercel's serverless functions.

The example functions as expected in a local development environment.

Solution:

The issue was resolved by:

  1. Moving the webhook route definition into the pages directory (to create an API route within Next.js).
  2. using the next-js adapter for webhookCallback.

Possible Cause:
The original setup might not be compatible with Vercel's serverless function execution model. or Next.js app directory API routes and the std/http adapter probably are not compatible.

Im trying to use grammy on my next.js project but whatever i do i get this

`npx ts-node scripts/build.ts

TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for /Users/mateokappa/Desktop/www/scripts/build.ts
at Object.getFileProtocolModuleFormat [as file:] (node:internal/modules/esm/get_format:160:9)
at defaultGetFormat (node:internal/modules/esm/get_format:203:36)
at defaultLoad (node:internal/modules/esm/load:141:22)
at async nextLoad (node:internal/modules/esm/hooks:865:22)
at async nextLoad (node:internal/modules/esm/hooks:865:22)
at async Hooks.load (node:internal/modules/esm/hooks:448:20)
at async MessagePort.handleMessage (node:internal/modules/esm/worker:196:18) {
code: 'ERR_UNKNOWN_FILE_EXTENSION'
}`

Deno deploy example

After deploying the bot on Deno Deploy, it works for a few hours and then requires sending an HTTP request to the webhook URL. I think this is not the behavior we expect.

need Nuxt3 example

I want to implement grammyjs in nuxt3.

Any help or guidance would be greatly appreciated.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.