简体   繁体   English

如何使用 Telegraf API 发送本地存储的照片?

[英]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.

相关问题 带有 telegraf.js 的 Telegram 机器人:无法使用 flickr api 发送随机照片进行聊天 - Telegram bot with telegraf.js : can't send random photo to chat using flickr api Telegraf 从 Telegram API 发送图像 - Telegraf send an image from Telegram API 如何使用 twilio 通过 whatsapp 发送本地存储的媒体文件 .. node js express - how to send locally stored media files using twilio over whatsapp .. node js express 如何使用 Telegraf.js Node.js 在 Telegram Bot 中发送私人消息 - How to send private message in Telegram Bot using Telegraf.js Node.js 如何使用 npm 库 Telegraf 从数据库中的 id 列表一次向所有电报机器人用户发送消息 - How to send messages to all telegram bot users from a list of ids in a database at once using npm library Telegraf 如何从电报机器人 api 发送照片(javascript) - how to send photo from telegram bot api (javascript) 通过Ajax将本地存储的图像发送到服务器 - Send Locally Stored Image to Server Via Ajax 使用本地存储的变量来清除interval - Using locally stored variable to clearInterval 如何使用 Reactjs 通过本地运行的 SMTP/IMAP 服务器发送 Email? - How to send an Email with a locally running SMTP/IMAP Server using Reactjs? 如何使用JS从Google Contacts API获取照片 - How to get a photo from Google Contacts API using JS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM