ruby-on-rails - 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"}"
解决方案
使用 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
推荐阅读
- java - ManyToOne 在创建 spring boot hiernate jpa 的两个表上给出 null
- javascript - 我尝试安装快速生成器,但出现此错误
- javascript - 如何使用反应路由器链接组件重新加载页面?
- java - 为什么 JVM tableswitch/lookupswitch 指令的格式有 0 到 3 个字节的填充?
- java - Spring Boot JPA - 错误地返回连接表数据的先前结果
- swift - swiftUI中子视图中的@StateObject和@ObservedObject有什么区别
- javascript - 离开页面后如何删除会话变量?
- reactjs - React - 将方向箭头设置为表格标题
- function - 哈希表不通过使用的函数返回值
- python - Python Scrapy text() 函数无法获取空的 td 元素