How to send photo on telegram bot whit QHttpMultiPart



  • Hi
    I want to send a photo to telegram bot with multipart
    this is telegram bot api about Send file
    how can i send photo to telegram bot in Qt

    As far as I know I should use QHttpMultiPart
    Please show me a example


  • Moderators

    Hi!

    QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
    
    QHttpPart textPart;
    textPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"text\""));
    textPart.setBody("my text");
    
    QHttpPart imagePart;
    imagePart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("image/jpeg"));
    imagePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"image\""));
    QFile *file = new QFile("image.jpg");
    file->open(QIODevice::ReadOnly);
    imagePart.setBodyDevice(file);
    file->setParent(multiPart); // we cannot delete the file now, so delete it with the multiPart
    
    multiPart->append(textPart);
    multiPart->append(imagePart);
    
    QUrl url("http://my.server.tld");
    QNetworkRequest request(url);
    
    QNetworkAccessManager manager;
    QNetworkReply *reply = manager.post(request, multiPart);
    multiPart->setParent(reply); // delete the multiPart with the reply
    // here connect signals etc.
    

    See: QHttpMultiPart Class



  • @Wieland I saw this before but i know how work, like 'setHeader' & 'setBody' what is that?


  • Moderators

    The header and body are part of the HTTP request you want to send.



  • @Wieland for send a photo to telegram bot i should do this :

    reply = manager->get(QNetworkRequest(QUrl("https://api.telegram.org/bot" +Token + "/sendPhoto?chat_id=" + chatID  + "&photo=" + photo + "&disable_notification=" + disableNotificationText)));
    

    but in QHttpMultiPart i dont know how to do this



  • Any one can help me

    how to do this in QHttpMultiPart :

    reply = manager->get(QNetworkRequest(QUrl("https://api.telegram.org/bot" +Token + "/sendPhoto?chat_id=" + chatID  + "&photo=" + photo + "&disable_notification=" + disableNotificationText)));
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.