Skip to content

Commit

Permalink
feat: add support Telegram Bot API 7.7
Browse files Browse the repository at this point in the history
  • Loading branch information
kravetsone committed Jul 7, 2024
1 parent 988c03e commit 27cd931
Show file tree
Hide file tree
Showing 10 changed files with 162 additions and 9 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -173,4 +173,5 @@ dist

# Finder (MacOS) folder config
.DS_Store
tg-bot-api
tg-bot-api
test.ts
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

Contexts is a great work of the [puregram](https://github.com/nitreojs/puregram) maintainer! Thank you for many code implementation and ideas. Forked since this [commit](https://github.com/nitreojs/puregram/commit/b431d9303de1696999e7f41f45d7c4d7d264c272). (Jan 28, 2024)

Currently, support [Telegram Bot API 7.6](https://core.telegram.org/bots/api-changelog#july-1-2024).
Currently, support [Telegram Bot API 7.7](https://core.telegram.org/bots/api-changelog#july-7-2024).

This library used under the hood in the GramIO framework (Please see [documentation](https://gramio.dev/)).

Expand Down
Binary file modified bun.lockb
Binary file not shown.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@gramio/contexts",
"version": "0.0.18",
"version": "0.0.19",
"main": "dist/index.js",
"keywords": [
"gramio",
Expand All @@ -17,13 +17,13 @@
],
"devDependencies": {
"@biomejs/biome": "1.7.3",
"@gramio/types": "^7.6.0",
"@gramio/types": "^7.7.0",
"@types/bun": "^1.1.6",
"common-tags": "^1.8.2",
"inspectable": "^3.0.2",
"madge": "^7.0.0",
"tsup": "^8.1.0",
"typescript": "^5.5.2"
"typescript": "^5.5.3"
},
"peerDependencies": {
"inspectable": "^3.0.1"
Expand Down
5 changes: 1 addition & 4 deletions scripts/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,7 @@ function snakeToCamelCase(str: string) {
}

const objectToGenerate: string[] = [
"PaidMediaInfo",
"PaidMediaPreview",
"PaidMediaPhoto",
"PaidMediaVideo",
"RefundedPayment",
// "ChatBackground",
// "BackgroundFill",
// "BackgroundType",
Expand Down
1 change: 1 addition & 0 deletions src/contexts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,4 @@ export * from "./boost-added";
export * from "./business-connection";
export * from "./business-messages-deleted";
export * from "./chat-background-set";
export * from "./refunded-payment";
95 changes: 95 additions & 0 deletions src/contexts/refunded-payment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import { inspectable } from "inspectable";

import type { TelegramObjects } from "@gramio/types";
import { Message } from "../structures/index";

import type { Constructor } from "../types";
import { applyMixins, memoizeGetters } from "../utils";

import { RefundedPayment } from "../structures/refunded-payment";
import type { BotLike } from "../types";
import { Context } from "./context";
import {
ChatActionMixin,
CloneMixin,
NodeMixin,
PinsMixin,
SendMixin,
TargetMixin,
} from "./mixins/index";

interface RefundedPaymentContextOptions<Bot extends BotLike> {
bot: Bot;
update: TelegramObjects.TelegramUpdate;
payload: TelegramObjects.TelegramMessage;
updateId: number;
}

/**
* This object contains basic information about a successful payment.
*
* [Documentation](https://core.telegram.org/bots/api/#RefundedPayment)
*/
class RefundedPaymentContext<Bot extends BotLike> extends Context<Bot> {
/** The raw data that is used for this Context */
payload: TelegramObjects.TelegramMessage;

constructor(options: RefundedPaymentContextOptions<Bot>) {
super({
bot: options.bot,
updateType: "refunded_payment",
updateId: options.updateId,
update: options.update,
});

this.payload = options.payload;
}

/** Received payment */
get eventPayment() {
return new RefundedPayment(
this.payload.refunded_payment as TelegramObjects.TelegramRefundedPayment,
);
}
}

interface RefundedPaymentContext<Bot extends BotLike>
extends Constructor<RefundedPaymentContext<Bot>>,
Message,
TargetMixin,
SendMixin<Bot>,
ChatActionMixin<Bot>,
NodeMixin<Bot>,
PinsMixin<Bot>,
CloneMixin<
Bot,
RefundedPaymentContext<Bot>,
RefundedPaymentContextOptions<Bot>
> {}
applyMixins(RefundedPaymentContext, [
Message,
TargetMixin,
SendMixin,
ChatActionMixin,
NodeMixin,
PinsMixin,
CloneMixin,
]);
memoizeGetters(RefundedPaymentContext, ["eventPayment"]);

inspectable(RefundedPaymentContext, {
serialize(context) {
return {
id: context.id,
from: context.from,
senderId: context.senderId,
createdAt: context.createdAt,
chat: context.chat,
chatId: context.chatId,
chatType: context.chatType,
eventPayment: context.eventPayment,
};
},
});

export { RefundedPaymentContext };
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ export const contextsMappings = {
general_forum_topic_unhidden: Contexts.GeneralForumTopicUnhiddenContext,
shipping_query: Contexts.ShippingQueryContext,
successful_payment: Contexts.SuccessfulPaymentContext,
refunded_payment: Contexts.RefundedPaymentContext,
users_shared: Contexts.UsersSharedContext,
chat_shared: Contexts.ChatSharedContext,
video_chat_ended: Contexts.VideoChatEndedContext,
Expand Down
56 changes: 56 additions & 0 deletions src/structures/refunded-payment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import type { TelegramObjects } from "@gramio/types";
import { Inspect, Inspectable } from "inspectable";

/**
* This object contains basic information about a refunded payment.
*
* [Documentation](https://core.telegram.org/bots/api/#refundedpayment)
*/
@Inspectable()
export class RefundedPayment {
constructor(public payload: TelegramObjects.TelegramRefundedPayment) {}

get [Symbol.toStringTag]() {
return this.constructor.name;
}

/**
* Three-letter ISO 4217 [currency](https://core.telegram.org/bots/payments#supported-currencies) code, or “XTR” for payments in [Telegram Stars](https://t.me/BotNews/90). Currently, always “XTR”
*/
@Inspect()
get currency() {
return this.payload.currency;
}

/**
* Total refunded price in the *smallest units* of the currency (integer, **not** float/double). For example, for a price of `US$ 1.45`, `total_amount = 145`. See the *exp* parameter in [currencies.json](https://core.telegram.org/bots/payments/currencies.json), it shows the number of digits past the decimal point for each currency (2 for the majority of currencies).
*/
@Inspect()
get totalAmount() {
return this.payload.total_amount;
}

/**
* Bot-specified invoice payload
*/
@Inspect()
get invoicePayload() {
return this.payload.invoice_payload;
}

/**
* Telegram payment identifier
*/
@Inspect()
get telegramPaymentChargeId() {
return this.payload.telegram_payment_charge_id;
}

/**
* *Optional*. Provider payment identifier
*/
@Inspect()
get providerPaymentChargeId() {
return this.payload.provider_payment_charge_id;
}
}
2 changes: 2 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ export type ContextsMapping<Bot extends BotLike> = {
general_forum_topic_unhidden: Contexts.GeneralForumTopicUnhiddenContext<Bot>;
shipping_query: Contexts.ShippingQueryContext<Bot>;
successful_payment: Contexts.SuccessfulPaymentContext<Bot>;
refunded_payment: Contexts.RefundedPaymentContext<Bot>;
users_shared: Contexts.UsersSharedContext<Bot>;
chat_shared: Contexts.ChatSharedContext<Bot>;
video_chat_ended: Contexts.VideoChatEndedContext<Bot>;
Expand Down Expand Up @@ -180,6 +181,7 @@ export type MessageEventName =
| "pinned_message"
| "invoice"
| "successful_payment"
| "refunded_payment"
| "users_shared"
| "chat_shared"
| "proximity_alert_triggered"
Expand Down

0 comments on commit 27cd931

Please sign in to comment.