Created
April 1, 2024 13:15
-
-
Save sojinsamuel/4ca861fff9711ddfb524a197649668df to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { config } from "dotenv"; | |
import TelegramBot from "node-telegram-bot-api"; | |
import getTwitterVideoURL from "./twitterfunction.js"; | |
config(); | |
const token = process.env.TELEGRAM_BOT_TOKEN; | |
// console.log(token); | |
const bot = new TelegramBot(token, { polling: true }); | |
bot.on("message", async (msg) => { | |
// console.log(JSON.stringify(msg, null, 2)); | |
const chatId = msg.chat.id; | |
const message = msg.text.trim(); | |
console.log(JSON.stringify(msg, null, 2)); | |
bot.sendInvoice( | |
chatId, | |
"Title goes here", | |
"Unlock premium content with a subscription", | |
"Invoice for payment", | |
"MY_PROVIDER_TOKEN", | |
"USD", | |
[{ label: "tax", amount: 4000 }], | |
{ | |
is_flexible: false, | |
need_phone_number: false, | |
need_email: true, | |
need_shipping_address: false, | |
} | |
); | |
}); | |
bot.on("pre_checkout_query", async (query) => { | |
console.log("Received pre-checkout query:", query); | |
// Answer the pre-checkout query within 10 seconds more info: https://core.telegram.org/bots/api#answerprecheckoutquery | |
const preCheckoutQueryAnswer = await bot.answerPreCheckoutQuery( | |
query.id, | |
true | |
); | |
console.log("Pre-checkout query answer:", preCheckoutQueryAnswer); | |
}); | |
bot.on("successful_payment", async (payload) => { | |
console.log("Successful payment:", payload); | |
const message = `Thank you for your payment of ${ | |
payload.total_amount / 100 | |
} ${payload.currency}!`; | |
await bot.sendMessage(payload.chat_id, message); | |
}); | |
bot.on("invoice", (error) => { | |
console.log("invoice", error); | |
}); | |
bot.on("shipping_query", async (query) => { | |
console.log("Received shipping query:", query); | |
// Don't have any shipping options, provide an empty array | |
const shippingOptions = []; | |
const shippingQueryAnswer = await bot.answerShippingQuery( | |
query.id, | |
true, // Set to false if the shipping cannot be processed | |
{ shipping_options: shippingOptions } | |
); | |
console.log("Shipping query answer:", shippingQueryAnswer); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment