Skip to content

Commit

Permalink
feat: fix miniapp
Browse files Browse the repository at this point in the history
  • Loading branch information
roziscoding committed Oct 11, 2023
1 parent 3aac709 commit f15c6fe
Show file tree
Hide file tree
Showing 7 changed files with 11 additions and 10 deletions.
1 change: 1 addition & 0 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export async function getBot(config: AppConfig, environment = Environment.Develo
/** Common middleware */
bot.use(loggerMiddleware(environment));
bot.use(sessionMiddleware(config, environment));
bot.use(qrCodeUrl(config));
bot.use(qrCodeUrl);

/** Conversations */
bot.use(grammyConversations());
Expand Down
2 changes: 1 addition & 1 deletion src/deps.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export * from "common-tags";
export * from "https://deno.land/[email protected]/encoding/base64.ts";
export { decodeBase64, encodeBase64 } from "https://deno.land/[email protected]/encoding/base64.ts";
export { Collection, MongoClient } from "https://deno.land/x/[email protected]/mod.ts";
export { debug } from "https://deno.land/x/[email protected]/mod.ts";
export * from "https://deno.land/x/[email protected]/mod.ts";
Expand Down
2 changes: 1 addition & 1 deletion src/endpoints/miniapp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const miniapp = () => {
document.addEventListener("DOMContentLoaded", () => {
const img = document.getElementById("img");
const search = new URLSearchParams(location.search);
const pixcode = search.get("pixcode");
const pixcode = search.get("tgWebAppStartParam");
img.src = "https://${config.WEBHOOK_URL}/qrcode?pixCode=" + pixcode;
})
</script>
Expand Down
6 changes: 3 additions & 3 deletions src/endpoints/qrcode.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { decode, json, qrcode } from "../deps.ts";
import { decodeBase64, json, qrcode } from "../deps.ts";

function createQrCode(content: string) {
return qrcode(content)
.then((codeString) => codeString.split(",")[1])
.then((codeString) => decode(codeString));
.then((codeString) => decodeBase64(codeString));
}

export async function getQRCode(req: Request) {
const url = new URL(req.url);
const pixCode = url.searchParams.get("pixCode");
const pixCode = new TextDecoder().decode(decodeBase64(url.searchParams.get("pixCode") ?? ""));

if (!pixCode) {
return json({ message: "missing pixCode param" }, { status: 422 });
Expand Down
2 changes: 1 addition & 1 deletion src/handlers/registered.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ composer
parse_mode: "HTML",
},
reply_markup: new InlineKeyboard()
.webApp("Visualizar QRCode", qrCodeUrl)
.url("Visualizar QRCode", qrCodeUrl)
.row()
.text("Marcar como concluído", `done_${finalValue}`),
},
Expand Down
6 changes: 3 additions & 3 deletions src/util/qr-code-url.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { AppConfig } from "../config.ts";
import type { Context, MiddlewareFn } from "../deps.ts";
import { encodeBase64 } from "../deps.ts";

export type QRCodeUrlFlavor = {
getQrCodeUrl: (pixCode: string) => string;
};

export const qrCodeUrl = (config: AppConfig): MiddlewareFn<Context & QRCodeUrlFlavor> => async (ctx, next) => {
ctx.getQrCodeUrl = (pixCode) => `https://${config.WEBHOOK_URL}/miniapp?pixcode=${encodeURIComponent(pixCode)}`;
export const qrCodeUrl: MiddlewareFn<Context & QRCodeUrlFlavor> = async (ctx, next) => {
ctx.getQrCodeUrl = (pixCode) => `https://t.me/${ctx.me.username}/qrcode?startapp=${encodeBase64(pixCode)}`;
await next();
};

0 comments on commit f15c6fe

Please sign in to comment.