简体   繁体   中英

Using setWebhook from Qt for a Telegram bot

Im developing a Telegram bot using Qt c++, and i'm having problems trying to set a Webhook.

SSL Server

First of all, i've created a ssl server using QTcpServer and QSslSocket. Some explanations about this can be found in the QSslSocket doc . Also, i generated a self-signed certificate as Telegram doc explains here , using the command:

openssl req -newkey rsa:2048 -sha256 -nodes -keyout YOURPRIVATE.key -x509 -days 365 -out YOURPUBLIC.pem -subj "/C=US/ST=New York/L=Brooklyn/O=Example Brooklyn Company/CN=YOURDOMAIN.EXAMPLE"

The result is a pair of files, a private.key file and a public.pem file. So, i used them in the QSslSockets to secure the connection.

The result of that is a ssl server capable of listen and accept connections. When i use a browser to connect to my ssl server, i obtain a warning about using a self-signed certificate (which i think is normal), but can connect to the server. From the server, i'm able of read the data that browser sent. So, i think the server side is good.

Request for setWebhook

In order to perform a request for setWebhook API method , i use QHttpMultipart class to create a MIME Multipart request. The API method needs the Url to be contacted and the public certificate. So, i use this code to generate the url parameter:

QList<QHttpPart> parameters;
QHttpPart urlPart;
urlPart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("text/plain"));
urlPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"url\""));
urlPart.setBody(_url.toLatin1());
parameters.append(urlPart);

And this code to generate the certificate parameter:

QHttpPart filePath;
    filePath.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"certificate\""));
    QFile *file = new QFile(_filePath);
    file->open(QIODevice::ReadOnly);
    filePath.setBodyDevice(file);
    file->setParent(this);
    parameters.append(filePath);

I receive a correct response, with the message that "the webhook was set". But, when Telegram connects to my ssl server, the ssl handshake doesn't finish in a right way (neither encrypted() nor sslError() signals are emitted). I think that the problem is the way i upload the public certificate. As you can see, for the file QHttpPart, i doesn't set the content-type header because i don't know what value to use. I don't know if this can be the problem. I use "text/plain" for url, but don't know what to use for the certificate file.

So, i don't know what could be my problem. Even, i'm not sure if it could be the file upload or not. Using a self signed certificate is not a problem, since the documentation indicates this as a valid way. Any help would be appreciated.

Thanks in advance.

I finally found the problem, and it was the content-type. I removed the content-type of the first param, the urlPart. And also added the content-type to the filePath, using as value "application/x-x509-ca-cert". It works like a charm now.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM