我正在使用 Node.js 构建一个电报机器人,人们可以在其中发送推文的 URL,以及它是否包含视频类型的媒体。它下载它并将其发送给用户。这就是我的目的,我已经完成了。
我遇到的问题是在此机器人帐户上设置付费墙,以便在 2 或 3 个请求后,与机器人交互的用户必须付款才能继续。
所以我使用了sendInvoice方法。
仅供参考,我没有使用任何外部库,我直接与 telegram api 端点交互。
我在测试模式下使用 stripe 进行付款(如果有帮助的话,我的 acc 尚未验证)。我遇到的问题正如你在这张图片中看到的:
发送发票后,用户单击付款按钮并添加所有必要的银行卡详细信息,然后点击付款。它不断缓冲然后超时。
我也尝试了 createInvoiceLink 方法,也发生了同样的事情。
当然,我的解决方案中有一些错误,但我不知道它在哪里。可能我必须使用网络钩子或其他东西来捕获结帐启动/完成。但我怎样才能让 Telegram api 了解我的支付 webhook 路径(假设存在类似的路径)。
我发现的唯一方法是方法 setWebhook,这是另一种轮询方法。这就是我在本地使用 ngrok 所做的事情。
我的代码的一部分,这是功能的抽象版本:
const app = express();
const port = 3000;
const botToken = process.env.TELEGRAM_BOT_TOKEN;
app.use(bodyParser.json());
// Webhook endpoint that receives incoming messages (exposed via ngrok)
app.post(`/bot${botToken}`, async (req, res) => {
const { message } = req.body;
console.log({ message });
if (message && message.text) {
const chatId = message.chat.id;
const messageText = message.text;
// after successfull 3 responses from the bot, send an invoice
const invoiceRes = await sendInvoice(
chatId,
"Premium Subscription",
"Unlock premium content with a subscription.",
"premium_subscription",
process.env.PROVIDER_TOKEN,
"subscription",
"USD",
[{ label: "Subscription", amount: 1000 }],
);
console.log(JSON.stringify(invoiceRes, null, 2));
}
res.status(200).end();
});
async function sendInvoice(
chatId,
title,
description,
payload,
providerToken,
startParameter,
currency,
prices,
) {
const apiUrl = `https://api.telegram.org/bot${botToken}/sendInvoice`;
const invoice = {
chat_id: chatId,
title: title,
description: description,
payload: payload,
provider_token: providerToken,
start_parameter: startParameter,
currency: currency,
prices: prices,
};
try {
const response = await fetch(apiUrl, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(invoice),
});
const data = await response.json();
// console.log(data);
return data;
} catch (error) {
console.error("There was a problem sending the invoice:", error);
return error.message;
}
}
我用谷歌搜索了很多东西来找到一些答案,文档的支付部分中没有指定任何内容,而不是获取提供者令牌(如果我必须在条带帐户仪表板上执行任何操作,例如添加 webhook 端点,则没有任何具体内容,即使是这种情况,telegram 如何知道这是它也应该进行通信的 webhook)
我经常提到 webhook,因为在我看来,我假设这是我缺少的解决方案。如果不是,我很抱歉这样做。我对条纹或构建电报机器人没有太多经验。我希望我能得到一些帮助来解决这个问题。如果您很忙,即使是一个小小的指导也足够了。我所要求的只是我可以接受的东西。