rubenlagus / TelegramBots

Java library to create bots using Telegram Bots API
https://telegram.me/JavaBotsApi
MIT License
4.68k stars 1.18k forks source link

workable example of get received photos #108

Closed ivolanski closed 8 years ago

ivolanski commented 8 years ago

Hello,

I an looking for something that seems to be simple but I can not manage it to work, could anyone provide me a example that receives a photo to a java.io file ?

Until now I have: ` public void onUpdateReceived(Update update) {

    if(update.getMessage().getPhoto()!=null){
        GetFile getFileRequest = new GetFile();

        getFileRequest.setFileId(update.getMessage().getPhoto().get(0).getFileId());

        try {
             org.telegram.telegrambots.api.objects.File file = getFile(getFileRequest);
            // saveUrl("teste.jpg", "https://api.telegram.org/"+getBotToken()+"/"+file.getFilePath());

            File arquivo = new File("https://api.telegram.org/"+getBotToken()+"/"+file.getFilePath());
            URL url = new URL("https://api.telegram.org/"+getBotToken()+"/"+file.getFilePath());
            FileUtils.copyURLToFile(url, arquivo);

            System.out.println(arquivo.getPath());
            //System.out.println("https://api.telegram.org/"+getBotToken()+"/getFile?file_id="+photo.getFileId());
            System.out.println(Photo.classificar(arquivo));`

Log says file is null: java.lang.NullPointerException at br.com.prime33.telegram.bot.oficial.principal.Photo.classificar(Photo.java:35) at br.com.prime33.telegram.bot.oficial.principal.Bela21Bot.onUpdateReceived(Bela21Bot.java:51) at org.telegram.telegrambots.updatesreceivers.BotSession$HandlerThread.run(BotSession.java:180)

neither this output path works when I try in the browser

https://api.telegram.org/246536565:AAFS1_A6sHcwYzksZLaeoqcOd-EC9dd3IYk/photo/file_13.jpg

Thanks in advance

ivolanski commented 8 years ago

the problem was the missing bot word before bot api https://api.telegram.org/bot246536565:AAFS1_A6sHcwYzksZLaeoqcOd-EC9dd3IYk/photo/file_13.jpg

solved the problem