ebeneditos / telegram.bot

Develop a Telegram Bot with R
https://ebeneditos.github.io/telegram.bot/
GNU General Public License v3.0
107 stars 24 forks source link

sendPhoto bad request 400 error #20

Closed krose closed 2 years ago

krose commented 2 years ago

Hi @ebeneditos ... thanks for all the work you have done on this package.

I have a problem that I'm not sure how to fix and hope that you might be able to help. I've been pushing messages to a group for a while without the bot listening. I can send text, but whenever I try to send a picture, then it doesn't work. Do you know if there is anything I can do to send pictures? The script below is basically a cron job running on a server I have, but the error reproduce both on Debian and Windows.

# set token and group / message ID
telegram_token <- <my-telegram-token>
telegram_id <- <telegram-id>

# save an image to send
gg <- ggplot(mtcars, aes(cyl, hp)) + geom_point()
ggplot2::ggsave(filename = "~/plot.png", plot = gg, device = "jpeg", dpi = 500, width = 30, height = 30, units = "cm")

# set bot token.
bot <- Bot(token = telegram_token)

# works
bot$send_message(chat_id = telegram_id, text = "test")

# doesn't work
bot$send_photo(chat_id = telegram_id, photo = "~/plot.png", caption = "test")

*Updated for typo.

ebeneditos commented 2 years ago

Hi @krose, thanks for the question, did you try sending the image to a particular chat? Or sending the image from an URL (e.g. https://telegram.org/img/t_logo.png)? Just to make sure it is not an image issue

krose commented 2 years ago

Hi @ebeneditos Thank you for the reply. It's much appriciated.

The code below - except for the plot part - is an excact snippet of some code that works with an active bot I have running on a Digital Ocean server that sends messages to the same group and it works fine. The code / call is basically the same, which is why I don't think there's anything wrong with the file.


flow_gassco_source_plot <- function(bot, update){

  flow_gg <- <long-code-for-my-plot>

  ggsave(filename = "~/plot.png", plot = flow_gg, device = "jpeg", dpi = 500, width = 30, height = 20, units = "cm")

  tel_res <- bot$sendPhoto(chat_id = update$message$chat_id,
                photo = "~/plot.png")
}

gieaggplot_handler <- CommandHandler("flowGasscoSource", flow_gassco_source_plot, username = bot$getMe()$username)
updater <- updater + gieaggplot_handler
ebeneditos commented 2 years ago

Hi, I have not been able to replicate the problem, as when I do it the Bot sends the photo properly. Actually, there is a test just to check that (look line 45 on the Bot tests) that passes as expected. Given this, I believe this is not a problem from the package, so I will proceed to close the issue, sorry if I cannot provide more help.