首页 > 解决方案 > sendMediaGroup 电报机器人 Ruby on Rails

问题描述

请你向我解释我的问题在哪里以及如何解决它,我是新手:/

我正在创建电报机器人,使用 sendMediaGroup 方法、Ruby on Rails 框架和所有 cURL 从本地存储向我发送图片。

这是我的代码:

@pictures = Dir.glob("../folder/*.png")
@pictures.each do |file|
    puts file
sh "curl -s -F media='[{\"type\": \"photo\", \"media\": \"attach://file\"}]' -H 'Content-Type:multipart/form-data' https://api.telegram.org/botToken/sendMediaGroup?chat_id=chat_id"
end
end

当我发送该请求时,我已经收到了,但是文件的路径是正确的:

"{"ok":false,"error_code":400,"description":"Bad Request: media not found"}"

标签: ruby-on-railstelegramtelegram-botfastlane

解决方案


使用 SendPhoto 和 while ... 做到这一点:

 path = '../folder/**/'  # wheres ** my random named directory
  $i = 0
  $countFiles = Dir[File.join(path, '**', '*')].count { |file| File.file?(file) } # count of files in directory
  $arrayNames = Dir[File.join(path, '**', '*')].select{ |f| File.file? f }.map{ |f| File.basename f } #pick a names from directory
  $pathDir = Dir[File.join(dir, '*')][0] #selects folder
  while $i < $countFiles do
      $file = $arrayNames[$i] #puts a name of files from directory
      curl --verbose -X POST --header 'Content-Type:multipart/form-data' 'https://api.telegram.org/botToken/sendPhoto' -F chat_id='chat_id' -F photo='@#{$pathDir}/#{$file}' -F caption='#{$file}'
      $i +=1
  end

推荐阅读