[英]How to send a photo stored locally using telegraf API?
I'm a beginner with this.我是这个的初学者。
I'm trying to develop a Telegram bot using Telegraf API .我正在尝试使用Telegraf API开发 Telegram 机器人。 I'm stuck when the user sends a certain command (in my case: '/details <number>'
) and the bot has to reply with a specific photo stored locally inside the bot folder (based on the number that user types).当用户发送某个命令(在我的例子中是: '/details <number>'
)时,我被卡住了,bot 必须用存储在本地 bot 文件夹中的特定照片(基于用户键入的数字)进行回复。
I managed to achieve it using telegram-bot-api but since the API are not that completed, I switched to Telegraf.我设法使用 Telegram-bot-api 实现了它,但由于 API 还没有完成,我切换到 Telegraf。 I tried to understand the official Telegraf documentation but I'm not able to fully understand it.我试图理解 Telegraf 的官方文档,但我无法完全理解它。
Looking at the official examples and copy it to a new project obviously worked.查看官方示例并将其复制到一个新项目显然有效。
I'm having trouble with Photos stored locally and even on understand how 'bot.command' works at this point.我在本地存储照片时遇到了问题,甚至无法理解“bot.command”此时的工作原理。
this one works: bot.command('text', ({replyWithPhoto}) => replyWithPhoto(url: <url>));
这个有效: bot.command('text', ({replyWithPhoto}) => replyWithPhoto(url: <url>));
When the user types: '/text' he gots the photo back based on the URL.当用户输入:'/text' 时,他会根据 URL 取回照片。 I thought it was the same for the photo stored locally, but it's not..我以为本地存储的照片是一样的,但事实并非如此..
var filePath = "./assets/monsters/"+monsterList[2].name+"/sample.png";
bot.command('text', ({replyWithPhoto}) => replyWithPhoto(filePath));
filepath I'm sure it's correct.文件路径我确定它是正确的。
The code right now:现在的代码:
bot.on('text', function(ctx){
var chat_id = ctx.from.id;
var text = ctx.message.text;
var index = 0;
var commandDetails="";
if (text/includes("/details ")){
try{
var split = text.split(' ');
index = Number(split[1]);
commandDetails = '/details'+index.toString();
}catch(e){}
}
switch(text){
case: 'bla bla bla';break;
case ''+commandDetails:
try{
var filePath = './a.png';
///read below **
}
}
})
///read here **: here if i put bot.command(blabla); ///在这里阅读 **: 如果我把 bot.command(blabla); It's not working, maybe because it's like 'waiting' for another message input i guess... I tried even with the url: same result.它不起作用,也许是因为它就像“等待”另一个消息输入我猜......我什至尝试使用 url: 相同的结果。
So, the first part of the code should be really bad but the concept is to have the switch case that works whenever the users type '/details <number>'
.因此,代码的第一部分应该非常糟糕,但其概念是让 switch 案例在用户键入'/details <number>'
时起作用。 I have a list that will grow bigger and bigger so I tried to do it as clean as possible.我有一个会越来越大的清单,所以我尽量把它做得干净。
How can I send Photo with this system?如何使用此系统发送照片? PS: Once i was using Telegram-bot-api, putting the function sendphoto function where it should be now it was working correctly. PS:一旦我使用 Telegram-bot-api,将函数 sendphoto 放在它应该在的位置,现在它可以正常工作了。
You need to pass source
instead of url
.您需要传递source
而不是url
。 There's an example on GitHub . GitHub 上有一个示例。
ctx.replyWithPhoto({ source: '/cats/cat1.jpeg' });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.