Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Sending a message to a Telegram bot
Forum Updated to NodeBB v4.3 + New Features

Sending a message to a Telegram bot

Scheduled Pinned Locked Moved Unsolved General and Desktop
41 Posts 4 Posters 6.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J jenya7

    @jsulm
    I see. I have sslserver.c, sslserver.h in the library.
    When break at bot->sendMessage and go deep down I get to

    QByteArray Networking::request(QString endpoint, ParameterList params, Networking::Method method)
    {
        if (endpoint.isEmpty()) {
            qWarning("Cannot do request without endpoint");
            return QByteArray();
        }
        if (m_token.isEmpty()) {
            qWarning("Cannot do request without a Telegram Bot Token");
            return QByteArray();
        }
    
        QNetworkRequest req;
        QUrl url = buildUrl(endpoint);
        req.setUrl(url);
    
    #ifdef DEBUG
        qDebug("HTTP request: %s", qUtf8Printable(req.url().toString()));
    #endif
    
        QEventLoop loop;
    
        QNetworkReply *reply;
    
        if (method == GET) {
            url.setQuery(parameterListToString(params));
            req.setUrl(url);
            reply = m_nam->get(req);
        } else if (method == POST) {
            req.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
            reply = m_nam->post(req, parameterListToString(params));
        } else if (method == UPLOAD) {
            QByteArray boundary = generateMultipartBoundary(params);
            req.setHeader(QNetworkRequest::ContentTypeHeader, "multipart/form-data; boundary=" + boundary);
            QByteArray requestData = generateMultipartFormData(params, boundary);
            req.setHeader(QNetworkRequest::ContentLengthHeader, requestData.length());
            reply = m_nam->post(req, requestData);
        } else {
            qCritical("No valid method!");
            reply = nullptr;
        }
    
        if (reply == nullptr) {
            qWarning("Reply is NULL");
            delete reply;
            return QByteArray();
        }
    
        QObject::connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
        loop.exec();
    
        if (reply->error() != QNetworkReply::NoError) {
            qCritical("%s", qPrintable(QString("[%1] %2").arg(reply->error()).arg(reply->errorString())));
            delete reply;
            return QByteArray();
        }
    
        QByteArray ret = reply->readAll();
        delete reply;
        return ret;
    }
    
    QUrl Networking::buildUrl(QString endpoint)
    {
        QUrl url = QUrl();
        url.setScheme("https");
        url.setHost(API_HOST);
        url.setPath("/bot" + m_token + endpoint);
    
        return url;
    }
    

    And get the error at

     if (reply->error() != QNetworkReply::NoError) {
            qCritical("%s", qPrintable(QString("[%1] %2").arg(reply->error()).arg(reply->errorString())));
            delete reply;
            return QByteArray();
        }
    

    But the function builds an URL and sends with POST method - the same way I do it manually.

    jsulmJ Offline
    jsulmJ Offline
    jsulm
    Lifetime Qt Champion
    wrote on last edited by
    #4

    @jenya7 said in Sending a message to a Telegram bot:

    I have sslserver.c, sslserver.h in the library

    You still need the OpenSSL libs...

    https://forum.qt.io/topic/113070/qt-code-of-conduct

    J 1 Reply Last reply
    0
    • jsulmJ jsulm

      @jenya7 said in Sending a message to a Telegram bot:

      I have sslserver.c, sslserver.h in the library

      You still need the OpenSSL libs...

      J Offline
      J Offline
      jenya7
      wrote on last edited by
      #5

      @jsulm said in Sending a message to a Telegram bot:

      @jenya7 said in Sending a message to a Telegram bot:

      I have sslserver.c, sslserver.h in the library

      You still need the OpenSSL libs...

      Where can I find it?
      I was suggested to add

      LIBS += "C:/Qt/Tools/OpenSSL/Win_x64/lib/libssl.lib"
      

      But there is no OpenSSL folder in my Qt/Tools.

      jsulmJ 1 Reply Last reply
      0
      • J jenya7

        @jsulm said in Sending a message to a Telegram bot:

        @jenya7 said in Sending a message to a Telegram bot:

        I have sslserver.c, sslserver.h in the library

        You still need the OpenSSL libs...

        Where can I find it?
        I was suggested to add

        LIBS += "C:/Qt/Tools/OpenSSL/Win_x64/lib/libssl.lib"
        

        But there is no OpenSSL folder in my Qt/Tools.

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #6

        @jenya7 Start the Qt Maintenance Tool and select OpenSSL there, install...

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        J 1 Reply Last reply
        0
        • jsulmJ jsulm

          @jenya7 Start the Qt Maintenance Tool and select OpenSSL there, install...

          J Offline
          J Offline
          jenya7
          wrote on last edited by
          #7

          @jsulm said in Sending a message to a Telegram bot:

          @jenya7 Start the Qt Maintenance Tool and select OpenSSL there, install...

          When I get to Setup - Qt stage I get
          There is an important update available, please run the udater first
          qt.png

          How do I run the updater?

          jsulmJ 1 Reply Last reply
          0
          • J jenya7

            @jsulm said in Sending a message to a Telegram bot:

            @jenya7 Start the Qt Maintenance Tool and select OpenSSL there, install...

            When I get to Setup - Qt stage I get
            There is an important update available, please run the udater first
            qt.png

            How do I run the updater?

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #8

            @jenya7 said in Sending a message to a Telegram bot:

            How do I run the updater?

            What about clicking on "Update components" and then "Next"?

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            J 1 Reply Last reply
            1
            • jsulmJ jsulm

              @jenya7 said in Sending a message to a Telegram bot:

              How do I run the updater?

              What about clicking on "Update components" and then "Next"?

              J Offline
              J Offline
              jenya7
              wrote on last edited by jenya7
              #9

              @jsulm said in Sending a message to a Telegram bot:

              @jenya7 said in Sending a message to a Telegram bot:

              How do I run the updater?

              What about clicking on "Update components" and then "Next"?

              Thank you. Now it's OK, OpenSSL installed, added LIBS += C:/Qt/Tools/OpenSSL/Win_x64/lib/libssl.lib
              But still the same problem - [99] TLS initialization failed

              well...in a Linux project I don't have this problem. probably openssl installed and handled properly in Linux.

              But when I run the code

              TelegramBot bot("my_api_key");
              bot.sendMessage("my_chat_id", "This is a test");
              

              I get in Debug window

              QUrl("https://api.telegram.org/botmy_api_key/sendMessage?chat_id=my_chat_id&text=This is a test")
              And nothing happens - my bot did not get the message.

              well...I've managed to send and get a message on Linux. actually it's good enough, my project should run on Linux.

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #10

                Hi,

                I would guess that your application cannot find the dlls at run time. You need to add the path to these to the PATH environment variable. Do that in the Run part of the Project panel.

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                J 1 Reply Last reply
                1
                • SGaistS SGaist

                  Hi,

                  I would guess that your application cannot find the dlls at run time. You need to add the path to these to the PATH environment variable. Do that in the Run part of the Project panel.

                  J Offline
                  J Offline
                  jenya7
                  wrote on last edited by
                  #11

                  @SGaist said in Sending a message to a Telegram bot:

                  Hi,

                  I would guess that your application cannot find the dlls at run time. You need to add the path to these to the PATH environment variable. Do that in the Run part of the Project panel.

                  I did so
                  qt.png

                  still the same problem - [99] TLS initialization failed

                  jsulmJ 1 Reply Last reply
                  0
                  • J jenya7

                    @SGaist said in Sending a message to a Telegram bot:

                    Hi,

                    I would guess that your application cannot find the dlls at run time. You need to add the path to these to the PATH environment variable. Do that in the Run part of the Project panel.

                    I did so
                    qt.png

                    still the same problem - [99] TLS initialization failed

                    jsulmJ Offline
                    jsulmJ Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by
                    #12

                    @jenya7 You need to add the path to the FOLDER containing the libs without the file name!

                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                    J 1 Reply Last reply
                    0
                    • jsulmJ jsulm

                      @jenya7 You need to add the path to the FOLDER containing the libs without the file name!

                      J Offline
                      J Offline
                      jenya7
                      wrote on last edited by
                      #13

                      @jsulm said in Sending a message to a Telegram bot:

                      @jenya7 You need to add the path to the FOLDER containing the libs without the file name!

                      OK. Did so. Removed the name. Still the same problem.

                      jsulmJ 1 Reply Last reply
                      0
                      • J jenya7

                        @jsulm said in Sending a message to a Telegram bot:

                        @jenya7 You need to add the path to the FOLDER containing the libs without the file name!

                        OK. Did so. Removed the name. Still the same problem.

                        jsulmJ Offline
                        jsulmJ Offline
                        jsulm
                        Lifetime Qt Champion
                        wrote on last edited by
                        #14

                        @jenya7 Does that folder also contain the dll file?

                        https://forum.qt.io/topic/113070/qt-code-of-conduct

                        J 1 Reply Last reply
                        0
                        • jsulmJ jsulm

                          @jenya7 Does that folder also contain the dll file?

                          J Offline
                          J Offline
                          jenya7
                          wrote on last edited by
                          #15

                          @jsulm said in Sending a message to a Telegram bot:

                          @jenya7 Does that folder also contain the dll file?

                          I ran a search in Qt folder on any ssl related files and found only
                          ssleay32.dll in C:\Qt\Tools\mingw730_64\opt\bin and C:\Qt\Tools\QtCreator\bin
                          Should I include the paths too?

                          1 Reply Last reply
                          0
                          • J Offline
                            J Offline
                            jenya7
                            wrote on last edited by
                            #16

                            I have a problem.
                            This way it works - if I initialize explicitly with a string

                            telebot::telebot(QObject *parent) : QObject(parent)
                            {
                                   TelegramBot bot("my_api_key");
                                   QObject::connect(&bot, &TelegramBot::newMessage, this, &telebot::NewMessage);
                            }
                            

                            But if I do it with variable

                            telebot::telebot(QObject *parent) : QObject(parent)
                            {
                               if (!sys_params.bot_api_key.isNull() && !sys_params.bot_api_key.isEmpty())
                               {
                                   TelegramBot bot(sys_params.bot_api_key);
                                   QObject::connect(&bot, &TelegramBot::newMessage, this, &telebot::NewMessage);
                               }
                            }
                            

                            I get the error
                            The inferior stopped because it received a signal from the operating system.
                            Signal name : SIGSEGV
                            Signal meaning : Segmentation fault

                            I see the constructor works before sys_params initialization although I set first line after window activation

                            int main(int argc, char *argv[])
                            {
                                QFuture<uint32_t> discovered;
                            
                                QApplication a(argc, argv);
                            
                                MainWindow w;
                                w.show();
                            
                                //here is sys_params initialization 
                                SetupRun();
                               
                                //some other code
                            
                                  return a.exec();
                            }
                            
                            

                            What should I do?

                            JonBJ 1 Reply Last reply
                            0
                            • J jenya7

                              I have a problem.
                              This way it works - if I initialize explicitly with a string

                              telebot::telebot(QObject *parent) : QObject(parent)
                              {
                                     TelegramBot bot("my_api_key");
                                     QObject::connect(&bot, &TelegramBot::newMessage, this, &telebot::NewMessage);
                              }
                              

                              But if I do it with variable

                              telebot::telebot(QObject *parent) : QObject(parent)
                              {
                                 if (!sys_params.bot_api_key.isNull() && !sys_params.bot_api_key.isEmpty())
                                 {
                                     TelegramBot bot(sys_params.bot_api_key);
                                     QObject::connect(&bot, &TelegramBot::newMessage, this, &telebot::NewMessage);
                                 }
                              }
                              

                              I get the error
                              The inferior stopped because it received a signal from the operating system.
                              Signal name : SIGSEGV
                              Signal meaning : Segmentation fault

                              I see the constructor works before sys_params initialization although I set first line after window activation

                              int main(int argc, char *argv[])
                              {
                                  QFuture<uint32_t> discovered;
                              
                                  QApplication a(argc, argv);
                              
                                  MainWindow w;
                                  w.show();
                              
                                  //here is sys_params initialization 
                                  SetupRun();
                                 
                                  //some other code
                              
                                    return a.exec();
                              }
                              
                              

                              What should I do?

                              JonBJ Offline
                              JonBJ Offline
                              JonB
                              wrote on last edited by
                              #17

                              @jenya7 said in Sending a message to a Telegram bot:

                              Signal name : SIGSEGV

                              What should I do?

                              You are supposed to run your code in a debugger. It will stop on the seg fault, go to the stack trace window to see information about where this is occurring from your code/the code you call.

                              Since we have no idea what your sys_params.bot_api_key is or what is in it, and since you say introducing it causes a seg fault, that would be my first suspect.....

                              telebot::telebot(QObject *parent) : QObject(parent)
                              {
                                     TelegramBot bot("my_api_key");
                                     QObject::connect(&bot, &TelegramBot::newMessage, this, &telebot::NewMessage);
                              }
                              

                              Your TelegramBot bot is a local variable, it will go out of scope and be destroyed at the end of this constructor. Same applies in your second code attempt. What do you think you are achieving with this code, because it is not right.

                              J 1 Reply Last reply
                              2
                              • JonBJ JonB

                                @jenya7 said in Sending a message to a Telegram bot:

                                Signal name : SIGSEGV

                                What should I do?

                                You are supposed to run your code in a debugger. It will stop on the seg fault, go to the stack trace window to see information about where this is occurring from your code/the code you call.

                                Since we have no idea what your sys_params.bot_api_key is or what is in it, and since you say introducing it causes a seg fault, that would be my first suspect.....

                                telebot::telebot(QObject *parent) : QObject(parent)
                                {
                                       TelegramBot bot("my_api_key");
                                       QObject::connect(&bot, &TelegramBot::newMessage, this, &telebot::NewMessage);
                                }
                                

                                Your TelegramBot bot is a local variable, it will go out of scope and be destroyed at the end of this constructor. Same applies in your second code attempt. What do you think you are achieving with this code, because it is not right.

                                J Offline
                                J Offline
                                jenya7
                                wrote on last edited by jenya7
                                #18

                                @JonB said in Sending a message to a Telegram bot:

                                @jenya7 said in Sending a message to a Telegram bot:

                                Signal name : SIGSEGV

                                What should I do?

                                You are supposed to run your code in a debugger. It will stop on the seg fault, go to the stack trace window to see information about where this is occurring from your code/the code you call.

                                Since we have no idea what your sys_params.bot_api_key is or what is in it, and since you say introducing it causes a seg fault, that would be my first suspect.....

                                telebot::telebot(QObject *parent) : QObject(parent)
                                {
                                       TelegramBot bot("my_api_key");
                                       QObject::connect(&bot, &TelegramBot::newMessage, this, &telebot::NewMessage);
                                }
                                

                                Your TelegramBot bot is a local variable, it will go out of scope and be destroyed at the end of this constructor. Same applies in your second code attempt. What do you think you are achieving with this code, because it is not right.

                                Yes I stop with a breakpoint at
                                if (!sys_params.bot_api_key.isNull() && !sys_params.bot_api_key.isEmpty())
                                the next step - refering to sys_params.bot_api_key generates the error.

                                It's a field of a structure

                                typedef struct
                                {
                                   //some fields
                                    QString bot_api_key;
                                }SYS_PARAMS;
                                
                                SYS_PARAMS sys_params;
                                

                                How can I do it global? It's not like I can instantiate it

                                TelegramBot bot;  
                                 telebot::telebot(QObject *parent) : QObject(parent) 
                                {
                                     bot = new TelegramBot ("my_api_key");  //is not allowed
                                }
                                
                                jsulmJ JonBJ 2 Replies Last reply
                                0
                                • J jenya7

                                  @JonB said in Sending a message to a Telegram bot:

                                  @jenya7 said in Sending a message to a Telegram bot:

                                  Signal name : SIGSEGV

                                  What should I do?

                                  You are supposed to run your code in a debugger. It will stop on the seg fault, go to the stack trace window to see information about where this is occurring from your code/the code you call.

                                  Since we have no idea what your sys_params.bot_api_key is or what is in it, and since you say introducing it causes a seg fault, that would be my first suspect.....

                                  telebot::telebot(QObject *parent) : QObject(parent)
                                  {
                                         TelegramBot bot("my_api_key");
                                         QObject::connect(&bot, &TelegramBot::newMessage, this, &telebot::NewMessage);
                                  }
                                  

                                  Your TelegramBot bot is a local variable, it will go out of scope and be destroyed at the end of this constructor. Same applies in your second code attempt. What do you think you are achieving with this code, because it is not right.

                                  Yes I stop with a breakpoint at
                                  if (!sys_params.bot_api_key.isNull() && !sys_params.bot_api_key.isEmpty())
                                  the next step - refering to sys_params.bot_api_key generates the error.

                                  It's a field of a structure

                                  typedef struct
                                  {
                                     //some fields
                                      QString bot_api_key;
                                  }SYS_PARAMS;
                                  
                                  SYS_PARAMS sys_params;
                                  

                                  How can I do it global? It's not like I can instantiate it

                                  TelegramBot bot;  
                                   telebot::telebot(QObject *parent) : QObject(parent) 
                                  {
                                       bot = new TelegramBot ("my_api_key");  //is not allowed
                                  }
                                  
                                  jsulmJ Offline
                                  jsulmJ Offline
                                  jsulm
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #19

                                  @jenya7 said in Sending a message to a Telegram bot:

                                  the next step - refering to sys_params.bot_api_key generates the error

                                  As @JonB asked: how does the stack trace look like after crash?

                                  "How can I do it global?" - why should it be global? Simply make bot a class member...

                                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                                  J 1 Reply Last reply
                                  1
                                  • J jenya7

                                    @JonB said in Sending a message to a Telegram bot:

                                    @jenya7 said in Sending a message to a Telegram bot:

                                    Signal name : SIGSEGV

                                    What should I do?

                                    You are supposed to run your code in a debugger. It will stop on the seg fault, go to the stack trace window to see information about where this is occurring from your code/the code you call.

                                    Since we have no idea what your sys_params.bot_api_key is or what is in it, and since you say introducing it causes a seg fault, that would be my first suspect.....

                                    telebot::telebot(QObject *parent) : QObject(parent)
                                    {
                                           TelegramBot bot("my_api_key");
                                           QObject::connect(&bot, &TelegramBot::newMessage, this, &telebot::NewMessage);
                                    }
                                    

                                    Your TelegramBot bot is a local variable, it will go out of scope and be destroyed at the end of this constructor. Same applies in your second code attempt. What do you think you are achieving with this code, because it is not right.

                                    Yes I stop with a breakpoint at
                                    if (!sys_params.bot_api_key.isNull() && !sys_params.bot_api_key.isEmpty())
                                    the next step - refering to sys_params.bot_api_key generates the error.

                                    It's a field of a structure

                                    typedef struct
                                    {
                                       //some fields
                                        QString bot_api_key;
                                    }SYS_PARAMS;
                                    
                                    SYS_PARAMS sys_params;
                                    

                                    How can I do it global? It's not like I can instantiate it

                                    TelegramBot bot;  
                                     telebot::telebot(QObject *parent) : QObject(parent) 
                                    {
                                         bot = new TelegramBot ("my_api_key");  //is not allowed
                                    }
                                    
                                    JonBJ Offline
                                    JonBJ Offline
                                    JonB
                                    wrote on last edited by JonB
                                    #20

                                    @jenya7
                                    As @jsulm has just written for the seg fault.

                                    TelegramBot bot;  
                                         bot = new TelegramBot ("my_api_key");  //is not allowed
                                    

                                    Why do you think this is "not allowed"? What does the compiler error message tell you, I would guess it's pretty clear?

                                    J 1 Reply Last reply
                                    0
                                    • jsulmJ jsulm

                                      @jenya7 said in Sending a message to a Telegram bot:

                                      the next step - refering to sys_params.bot_api_key generates the error

                                      As @JonB asked: how does the stack trace look like after crash?

                                      "How can I do it global?" - why should it be global? Simply make bot a class member...

                                      J Offline
                                      J Offline
                                      jenya7
                                      wrote on last edited by
                                      #21

                                      @jsulm said in Sending a message to a Telegram bot:

                                      @jenya7 said in Sending a message to a Telegram bot:

                                      the next step - refering to sys_params.bot_api_key generates the error

                                      As @JonB asked: how does the stack trace look like after crash?

                                      "How can I do it global?" - why should it be global? Simply make bot a class member...

                                      It stops in qstring.h at
                                      inline bool QString::isEmpty()
                                      const { return d->size == 0; }

                                      jsulmJ JonBJ 2 Replies Last reply
                                      0
                                      • J jenya7

                                        @jsulm said in Sending a message to a Telegram bot:

                                        @jenya7 said in Sending a message to a Telegram bot:

                                        the next step - refering to sys_params.bot_api_key generates the error

                                        As @JonB asked: how does the stack trace look like after crash?

                                        "How can I do it global?" - why should it be global? Simply make bot a class member...

                                        It stops in qstring.h at
                                        inline bool QString::isEmpty()
                                        const { return d->size == 0; }

                                        jsulmJ Offline
                                        jsulmJ Offline
                                        jsulm
                                        Lifetime Qt Champion
                                        wrote on last edited by
                                        #22

                                        @jenya7 This not stack trace...

                                        https://forum.qt.io/topic/113070/qt-code-of-conduct

                                        J 1 Reply Last reply
                                        1
                                        • JonBJ JonB

                                          @jenya7
                                          As @jsulm has just written for the seg fault.

                                          TelegramBot bot;  
                                               bot = new TelegramBot ("my_api_key");  //is not allowed
                                          

                                          Why do you think this is "not allowed"? What does the compiler error message tell you, I would guess it's pretty clear?

                                          J Offline
                                          J Offline
                                          jenya7
                                          wrote on last edited by jenya7
                                          #23

                                          @JonB said in Sending a message to a Telegram bot:

                                          @jenya7
                                          As @jsulm has just written for the seg fault.

                                          TelegramBot bot;  
                                               bot = new TelegramBot ("my_api_key");  //is not allowed
                                          

                                          Why do you think this is "not allowed"? What does the compiler error message tell you, I would guess it's pretty clear?

                                          TelegramBot bot; //here - error: no matching constructor for initialization of 'TelegramBot'

                                          bot = new TelegramBot ("my_api_key");

                                          jsulmJ 1 Reply Last reply
                                          0

                                          • Login

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • Users
                                          • Groups
                                          • Search
                                          • Get Qt Extensions
                                          • Unsolved