grammyjs / grammY

The Telegram Bot Framework.
https://grammy.dev
MIT License
2.21k stars 110 forks source link

Can't run grammY on Cloudflare Workers / wrangler locally #368

Closed dandaka closed 1 year ago

dandaka commented 1 year ago

How to reproduce

  1. Create a blank workers project with Wrangler
  2. Add grammy
  3. Run wrangler dev --local
✘ [ERROR] Could not resolve "http"

    node_modules/grammy/out/platform.node.js:5:23:
      5 │ const http_1 = require("http");
        ╵                        ~~~~~~

  The package "http" wasn't found on the file system but is built into node.
  Add "node_compat = true" to your wrangler.toml file to enable Node compatibility.

✘ [ERROR] Could not resolve "https"

    node_modules/grammy/out/platform.node.js:6:24:
      6 │ const https_1 = require("https");
        ╵                         ~~~~~~~

  The package "https" wasn't found on the file system but is built into node.
  Add "node_compat = true" to your wrangler.toml file to enable Node compatibility.

✘ [ERROR] Could not resolve "stream"

    node_modules/grammy/out/platform.node.js:7:25:
      7 │ const stream_1 = require("stream");
        ╵                          ~~~~~~~~

  The package "stream" wasn't found on the file system but is built into node.
  Add "node_compat = true" to your wrangler.toml file to enable Node compatibility.

✘ [ERROR] Could not resolve "fs"

    node_modules/grammy/out/types.node.js:18:21:
      18 │ const fs_1 = require("fs");
         ╵                      ~~~~

  The package "fs" wasn't found on the file system but is built into node.
  Add "node_compat = true" to your wrangler.toml file to enable Node compatibility.

✘ [ERROR] Could not resolve "path"

    node_modules/grammy/out/types.node.js:20:23:
      20 │ const path_1 = require("path");
         ╵                        ~~~~~~

  The package "path" wasn't found on the file system but is built into node.
  Add "node_compat = true" to your wrangler.toml file to enable Node compatibility.

✘ [ERROR] Build failed with 5 errors:

  node_modules/grammy/out/platform.node.js:5:23: ERROR: Could not resolve "http"
  node_modules/grammy/out/platform.node.js:6:24: ERROR: Could not resolve "https"
  node_modules/grammy/out/platform.node.js:7:25: ERROR: Could not resolve "stream"
  node_modules/grammy/out/types.node.js:18:21: ERROR: Could not resolve "fs"
  node_modules/grammy/out/types.node.js:20:23: ERROR: Could not resolve "path"

If I add node_compat = true, I get another error

[mf:err] TypeError: globalThis.XMLHttpRequest is not a constructor

/node_modules/rollup-plugin-node-polyfills/polyfills/http-lib/capability.js:20
    xhr = new global.XMLHttpRequest()
          ^
TypeError: globalThis.XMLHttpRequest is not a constructor
KnorpelSenf commented 1 year ago

You need to import from grammy/web. A guide for this is in the works. You can look at a preview of it at https://github.com/grammyjs/website/pull/587#issuecomment-1407387869

dandaka commented 1 year ago

Hey @KnorpelSenf, thank you for helping out. Do you think it is possible to use grammyjs with a router library? The most popular on Workers is this one.

https://github.com/kwhitley/itty-router

I was trying to call webhookCallback(bot, "cloudflare") with little success. Sorry if my question is too noob.

KnorpelSenf commented 1 year ago

Yes, there is a way to do this. I'm not familiar with that project. Can you share the code that you used when you tried to integrate the two libraries?

dandaka commented 1 year ago

This is the original way your documentation suggests using Grammy, it works on Workers just fine https://github.com/dandaka/workers-grammy/blob/main/src/index.ts

This is a version with a router that does not work https://github.com/dandaka/workers-grammy/blob/main/src/index-router.ts

I assume there should be a way to link 2 different event listeners, but could not find a way yet

dandaka commented 1 year ago

Also when I call webhookCallback(bot, "cloudflare");

I get the following TS error in my IDE:

No overload matches this call.
  Overload 1 of 2, '(bot: Bot<Context, Api<RawApi>>, adapter?: "callback" | "express" | "koa" | "fastify" | "std/http" | "oak" | "hono" | "http" | "https" | "aws-lambda" | "azure" | "next-js" | "sveltekit" | FrameworkAdapter | undefined, onTimeout?: "throw" | ... 2 more ... | undefined, timeoutMilliseconds?: number | undefined, secretToken?: string | undefined): (...args: any[]) => any', gave the following error.
    Argument of type '"cloudflare"' is not assignable to parameter of type '"callback" | "express" | "koa" | "fastify" | "std/http" | "oak" | "hono" | "http" | "https" | "aws-lambda" | "azure" | "next-js" | "sveltekit" | FrameworkAdapter | undefined'.
  Overload 2 of 2, '(bot: Bot<Context, Api<RawApi>>, adapter?: "callback" | "express" | "koa" | "fastify" | "std/http" | "oak" | "hono" | "http" | "https" | "aws-lambda" | "azure" | "next-js" | "sveltekit" | FrameworkAdapter | undefined, webhookOptions?: WebhookOptions | undefined): (...args: any[]) => any', gave the following error.
    Argument of type '"cloudflare"' is not assignable to parameter of type '"callback" | "express" | "koa" | "fastify" | "std/http" | "oak" | "hono" | "http" | "https" | "aws-lambda" | "azure" | "next-js" | "sveltekit" | FrameworkAdapter | undefined'.

The code runs fine on Workers thou. Should I update the grammY library version?

KnorpelSenf commented 1 year ago

Using the latest version is always a good idea.

I'm not sure why the type error happens, I'll ask the person who contributed the adapter about that.

Thanks for sharing the code snippets. It looks to me like the router expects handlers in the form of (req: Request) => Promise<Response>. Coincidentally, this is also the function signature that event handlers in worker modules expect. Can you try using the "cloudflare-mod" adapter and report back if that does the job? It isn't documented at https://grammy.dev/guide/deployment-types.html#web-framework-adapters yet but I'll include your experience when I get to edit the page. (Or do you feel like adding it to the docs if you can make it work?)

dandaka commented 1 year ago

I have managed to launch Grammy with honojs/hono router, works great.