Skip to content

Instantly share code, notes, and snippets.

@fipso
Last active April 8, 2022 13:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fipso/bca03a3bf2763727f5cc0f3dd6541b0f to your computer and use it in GitHub Desktop.
Save fipso/bca03a3bf2763727f5cc0f3dd6541b0f to your computer and use it in GitHub Desktop.
validate telegram bot login using nodejs
//Validate data
const pairs = Object.entries(tg)
.filter((e) => e[0] !== "hash")
.sort((a: any, b: any) => a[0].localeCompare(b[0]));
const tgData = pairs.map((p: any) => p.join("=")).join("\n");
console.log(tgData);
const key = crypto
.createHash("sha256")
.update(process.env.TELEGRAM_BOT_TOKEN!)
.digest();
console.log("key", key);
const dataHash = crypto
.createHmac("sha256", key)
.update(tgData)
.digest("hex");
console.log("our hash", dataHash);
if (dataHash !== tg.hash)
return res.status(400).json({ error: "invalid hash" });
console.log("hash ok");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment