From 1b4b2eb3539d955165edbdf050bab1d55d974029 Mon Sep 17 00:00:00 2001 From: KnorpelSenf Date: Fri, 3 Feb 2023 19:52:18 +0100 Subject: [PATCH] feat: support Bot API 6.5 (#363) --- package.json | 2 +- src/convenience/keyboard.ts | 46 ++++++++++++++++++++++++++++++- src/filter.ts | 2 ++ src/platform.deno.ts | 2 +- src/platform.web.ts | 2 +- src/types.deno.ts | 8 +++--- src/types.web.ts | 6 ++-- test/bot.test.ts | 2 +- test/composer.test.ts | 6 ++-- test/context.test.ts | 4 +-- test/convenience/keyboard.test.ts | 12 +++++++- test/convenience/session.test.ts | 1 + test/filter.test.ts | 4 +-- 13 files changed, 77 insertions(+), 20 deletions(-) diff --git a/package.json b/package.json index 123021df..cb249126 100644 --- a/package.json +++ b/package.json @@ -18,7 +18,7 @@ "contribs": "all-contributors" }, "dependencies": { - "@grammyjs/types": "^2.11.0", + "@grammyjs/types": "^2.12.0", "abort-controller": "^3.0.0", "debug": "^4.3.4", "node-fetch": "^2.6.7" diff --git a/src/convenience/keyboard.ts b/src/convenience/keyboard.ts index 3dec75c8..162be64a 100644 --- a/src/convenience/keyboard.ts +++ b/src/convenience/keyboard.ts @@ -1,6 +1,8 @@ import { type InlineKeyboardButton, type KeyboardButton, + type KeyboardButtonRequestChat, + type KeyboardButtonRequestUser, type LoginUrl, } from "../types.ts"; @@ -101,6 +103,48 @@ export class Keyboard { text(text: string) { return this.add({ text }); } + /** + * Adds a new request user button. When the user presses the button, a list + * of suitable users will be opened. Tapping on any user will send their + * identifier to the bot in a “user_shared” service message. Available in + * private chats only. + * + * @param text The text to display + * @param requestId A signed 32-bit identifier of the request + * @param options Options object for further requirements + */ + requestUser( + text: string, + requestId: number, + options: Omit = {}, + ) { + return this.add({ + text, + request_user: { request_id: requestId, ...options }, + }); + } + /** + * Adds a new request chat button. When the user presses the button, a list + * of suitable users will be opened. Tapping on a chat will send its + * identifier to the bot in a “chat_shared” service message. Available in + * private chats only. + * + * @param text The text to display + * @param requestId A signed 32-bit identifier of the request + * @param options Options object for further requirements + */ + requestChat( + text: string, + requestId: number, + options: Omit = { + chat_is_channel: false, + }, + ) { + return this.add({ + text, + request_chat: { request_id: requestId, ...options }, + }); + } /** * Adds a new contact request button. The user's phone number will be sent * as a contact when the button is pressed. Available in private chats only. @@ -135,7 +179,7 @@ export class Keyboard { * user presses the button. The Web App will be able to send a * “web_app_data” service message. Available in private chats only. * - * @param text Text text to display + * @param text The text to display * @param url An HTTPS URL of a Web App to be opened with additional data */ webApp(text: string, url: string) { diff --git a/src/filter.ts b/src/filter.ts index ad61b14d..d2db8f63 100644 --- a/src/filter.ts +++ b/src/filter.ts @@ -293,6 +293,8 @@ const MESSAGE_KEYS = { migrate_to_chat_id: {}, migrate_from_chat_id: {}, successful_payment: {}, + user_shared: {}, + chat_shared: {}, connected_website: {}, write_access_allowed: {}, passport_data: {}, diff --git a/src/platform.deno.ts b/src/platform.deno.ts index e35a44bc..57f0e710 100644 --- a/src/platform.deno.ts +++ b/src/platform.deno.ts @@ -17,7 +17,7 @@ if (isDeno) { // === Export system-specific operations // Turn an AsyncIterable into a stream -export { readableStreamFromIterable as itrToStream } from "https://deno.land/std@0.170.0/streams/mod.ts"; +export { readableStreamFromIterable as itrToStream } from "https://deno.land/std@0.176.0/streams/mod.ts"; // === Base configuration for `fetch` calls export const baseFetchConfig = (_apiRoot: string) => ({}); diff --git a/src/platform.web.ts b/src/platform.web.ts index 566bb39d..af71f5d2 100644 --- a/src/platform.web.ts +++ b/src/platform.web.ts @@ -3,7 +3,7 @@ export { d as debug }; // === Export system-specific operations // Turn an AsyncIterable into a stream -export { readableStreamFromIterable as itrToStream } from "https://deno.land/std@0.170.0/streams/mod.ts"; +export { readableStreamFromIterable as itrToStream } from "https://deno.land/std@0.176.0/streams/mod.ts"; // === Base configuration for `fetch` calls export const baseFetchConfig = (_apiRoot: string) => ({}); diff --git a/src/types.deno.ts b/src/types.deno.ts index 58f016a0..8f8e14e8 100644 --- a/src/types.deno.ts +++ b/src/types.deno.ts @@ -1,13 +1,13 @@ // === Needed imports -import { basename } from "https://deno.land/std@0.170.0/path/mod.ts"; -import { iterateReader } from "https://deno.land/std@0.170.0/streams/mod.ts"; -import { type InputFileProxy } from "https://esm.sh/@grammyjs/types@2.11.0"; +import { basename } from "https://deno.land/std@0.176.0/path/mod.ts"; +import { iterateReader } from "https://deno.land/std@0.176.0/streams/mod.ts"; +import { type InputFileProxy } from "https://esm.sh/@grammyjs/types@2.12.0"; import { debug as d, isDeno, toRaw } from "./platform.deno.ts"; const debug = d("grammy:warn"); // === Export all API types -export * from "https://esm.sh/@grammyjs/types@2.11.0"; +export * from "https://esm.sh/@grammyjs/types@2.12.0"; /** Something that looks like a URL. */ interface URLLike { diff --git a/src/types.web.ts b/src/types.web.ts index 72826b9f..e1309e3d 100644 --- a/src/types.web.ts +++ b/src/types.web.ts @@ -1,10 +1,10 @@ // === Needed imports -import { basename } from "https://deno.land/std@0.170.0/path/mod.ts"; -import { type InputFileProxy } from "https://esm.sh/@grammyjs/types@2.11.0"; +import { basename } from "https://deno.land/std@0.176.0/path/mod.ts"; +import { type InputFileProxy } from "https://esm.sh/@grammyjs/types@2.12.0"; import { toRaw } from "./platform.deno.ts"; // === Export all API types -export * from "https://esm.sh/@grammyjs/types@2.11.0"; +export * from "https://esm.sh/@grammyjs/types@2.12.0"; /** Something that looks like a URL. */ interface URLLike { diff --git a/test/bot.test.ts b/test/bot.test.ts index 53a44cf4..4517045b 100644 --- a/test/bot.test.ts +++ b/test/bot.test.ts @@ -2,7 +2,7 @@ import { Bot } from "../src/bot.ts"; import { assertEquals, assertThrows, -} from "https://deno.land/std@0.170.0/testing/asserts.ts"; +} from "https://deno.land/std@0.176.0/testing/asserts.ts"; function createBot(token: string) { return new Bot(token); diff --git a/test/composer.test.ts b/test/composer.test.ts index 0d2280fb..ff791404 100644 --- a/test/composer.test.ts +++ b/test/composer.test.ts @@ -3,13 +3,13 @@ import { type Context } from "../src/mod.ts"; import { assertEquals, assertRejects, -} from "https://deno.land/std@0.170.0/testing/asserts.ts"; -import { type Spy, spy } from "https://deno.land/std@0.170.0/testing/mock.ts"; +} from "https://deno.land/std@0.176.0/testing/asserts.ts"; +import { type Spy, spy } from "https://deno.land/std@0.176.0/testing/mock.ts"; import { beforeEach, describe, it, -} from "https://deno.land/std@0.170.0/testing/bdd.ts"; +} from "https://deno.land/std@0.176.0/testing/bdd.ts"; describe("Composer", () => { let composer: Composer; diff --git a/test/context.test.ts b/test/context.test.ts index a448911d..bd1ff1df 100644 --- a/test/context.test.ts +++ b/test/context.test.ts @@ -12,8 +12,8 @@ import { assertEquals, assertFalse, assertThrows, -} from "https://deno.land/std@0.170.0/testing/asserts.ts"; -import { describe, it } from "https://deno.land/std@0.170.0/testing/bdd.ts"; +} from "https://deno.land/std@0.176.0/testing/asserts.ts"; +import { describe, it } from "https://deno.land/std@0.176.0/testing/bdd.ts"; describe("Context", () => { const u = { id: 42, first_name: "bot", is_bot: true } as User; diff --git a/test/convenience/keyboard.test.ts b/test/convenience/keyboard.test.ts index be4cb15a..3467e3c3 100644 --- a/test/convenience/keyboard.test.ts +++ b/test/convenience/keyboard.test.ts @@ -25,13 +25,23 @@ describe("Keyboard", () => { .requestContact("contact") .requestLocation("location") .requestPoll("poll", "quiz") - .webApp("web app", "https://grammy.dev"); + .webApp("web app", "https://grammy.dev") + .requestUser("user", 12, { user_is_bot: true }) + .requestChat("chat", 42); assertEquals(keyboard.build(), [[ { text: "button" }, { text: "contact", request_contact: true }, { text: "location", request_location: true }, { text: "poll", request_poll: { type: "quiz" } }, { text: "web app", web_app: { url: "https://grammy.dev" } }, + { + text: "user", + request_user: { request_id: 12, user_is_bot: true }, + }, + { + text: "chat", + request_chat: { request_id: 42, chat_is_channel: false }, + }, ]]); }); diff --git a/test/convenience/session.test.ts b/test/convenience/session.test.ts index f37a1187..9a315450 100644 --- a/test/convenience/session.test.ts +++ b/test/convenience/session.test.ts @@ -739,6 +739,7 @@ describe("enhanceStorage", () => { storage: new MemorySessionStorage>(), millisecondsToLive: TICK_MS, }); + assertEquals(await store.read("k"), undefined); await store.write("k", 42); assertEquals(await store.read("k"), 42); await tick(2); diff --git a/test/filter.test.ts b/test/filter.test.ts index 724cdb1a..3c3e2540 100644 --- a/test/filter.test.ts +++ b/test/filter.test.ts @@ -3,8 +3,8 @@ import { type Context } from "../src/mod.ts"; import { assert, assertThrows, -} from "https://deno.land/std@0.170.0/testing/asserts.ts"; -import { describe, it } from "https://deno.land/std@0.170.0/testing/bdd.ts"; +} from "https://deno.land/std@0.176.0/testing/asserts.ts"; +import { describe, it } from "https://deno.land/std@0.176.0/testing/bdd.ts"; describe("matchFilter", () => { it("should reject empty filters", () => {