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 Deno.serveHttp adapter for webhookCallback #91

Merged
merged 4 commits into from
Nov 29, 2021
Merged

Add Deno.serveHttp adapter for webhookCallback #91

merged 4 commits into from
Nov 29, 2021

Conversation

rojvv
Copy link
Member

@rojvv rojvv commented Nov 19, 2021

Example usage:

import {
  Bot,
  webhookCallback,
} from "https://raw.githubusercontent.com/rojserbest/grammY/main/src/mod.ts";

const bot = new Bot('') // <-- your bot token here;
const handleUpdate = webhookCallback(bot, "serveHttp");

bot.command("start", (ctx) => ctx.reply("Hi!"));

const server = Deno.listen({ port: 8000 });

for await (const conn of server) {
  serveHttp(conn);
}

async function serveHttp(conn: Deno.Conn) {
  const httpConn = Deno.serveHttp(conn);

  for await (const requestEvent of httpConn) {
    if (requestEvent.request.method == "POST") {
      await handleUpdate(requestEvent);
    } else {
      await requestEvent.respondWith(new Response(undefined, { status: 200 }));
    }
  }
}

@KnorpelSenf
Copy link
Member

This will have to wait until #61 is merged, which will largely refactor how adapters are organised.

@rojvv
Copy link
Member Author

rojvv commented Nov 19, 2021

We'll wait. But what will be done to std/http?

@KnorpelSenf
Copy link
Member

The API I desire is to have a bunch of framework adapters supported out of the box and accessible via simple string identifiers. If you're using some esoteric stuff that grammY doesn't support, you can still pass your own definition. I am still waiting for feedback on this.

what will be done to std/http?

What do you mean?

@rojvv
Copy link
Member Author

rojvv commented Nov 19, 2021

Feedback on how adapters should work?

@rojvv
Copy link
Member Author

rojvv commented Nov 19, 2021

What do you mean?

I mean: will we support both serveHttp and std/http, separately?

@KnorpelSenf
Copy link
Member

Feedback on my suggestion that it's superfluous to expose 20 framework adapters when all you can do with them is to pass them to the same function. They shouldn't be accessible as objects.

Continue this here: #61 (comment)

@KnorpelSenf
Copy link
Member

What do you mean?

I mean: will we support both serveHttp and std/http, separately?

They're different abstractions. Why not?

@KnorpelSenf KnorpelSenf added the enhancement New feature or request label Nov 22, 2021
@KnorpelSenf
Copy link
Member

Please resolve the conflicts.

@rojvv
Copy link
Member Author

rojvv commented Nov 29, 2021

Done!

Copy link
Member

@KnorpelSenf KnorpelSenf left a comment

Choose a reason for hiding this comment

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

Is it correct that this can be used without explicitly responding to the request manually? I'd expect the created callback to be able to handle this automatically, and if I understand correctly, that's the case.

@rojvv
Copy link
Member Author

rojvv commented Nov 29, 2021

Sorry, I don't understand you. What is the "this" in "this can be used"?

@KnorpelSenf
Copy link
Member

Sorry for being ambiguous. Does this work?

import {
  Bot,
  webhookCallback,
} from "https://raw.githubusercontent.com/rojserbest/grammY/main/src/mod.ts";

const bot = new Bot('') // <-- your bot token here;
const handleUpdate = webhookCallback(bot, "serveHttp");

bot.command("start", (ctx) => ctx.reply("Hi!"));

const server = Deno.listen({ port: 8000 });

for await (const conn of server) {
  serveHttp(conn);
}

async function serveHttp(conn: Deno.Conn) {
  const httpConn = Deno.serveHttp(conn);

  for await (const requestEvent of httpConn) {
    if (requestEvent.request.method == "POST") {
      await handleUpdate(requestEvent);
    }
  }
}

@rojvv
Copy link
Member Author

rojvv commented Nov 29, 2021

Sure it does. You can even test it fast using Deno deploy.

@KnorpelSenf
Copy link
Member

So there's no reason you called await requestEvent.respondWith(new Response(undefined, { status: 200 }));?

Copy link
Member

@KnorpelSenf KnorpelSenf left a comment

Choose a reason for hiding this comment

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

LGTM

@KnorpelSenf KnorpelSenf merged commit c2be9d2 into grammyjs:main Nov 29, 2021
@KnorpelSenf
Copy link
Member

@all-contributors add @rojserbest for code

@allcontributors
Copy link
Contributor

@KnorpelSenf

I've put up a pull request to add @rojserbest! 🎉

@KnorpelSenf
Copy link
Member

KnorpelSenf commented Nov 30, 2021

But now you're still responding to the request twice, aren't you? Once inside the adapter, and once afterwards in the try block

@rojvv
Copy link
Member Author

rojvv commented Nov 30, 2021

That line would make sense if someone sent a get request, so they would know that the server is running. After all, it's just an example and you can remove that line and it will still work.

@KnorpelSenf
Copy link
Member

It will also run on POST requests

@KnorpelSenf
Copy link
Member

KnorpelSenf commented Nov 30, 2021

Now it's good

@rojvv
Copy link
Member Author

rojvv commented Nov 30, 2021

My bad, I'm lazy.

@KnorpelSenf
Copy link
Member

No worries, just wanted to know if I understood it correctly

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

Successfully merging this pull request may close these issues.

None yet

2 participants