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

Sending a message to a Telegram bot

Scheduled Pinned Locked Moved Unsolved General and Desktop
41 Posts 4 Posters 6.1k Views
  • 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 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 Online
    jsulmJ Online
    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 Online
              jsulmJ Online
              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 Online
                    jsulmJ Online
                    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
                      • J jenya7

                        @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 Online
                        jsulmJ Online
                        jsulm
                        Lifetime Qt Champion
                        wrote on last edited by
                        #24

                        @jenya7 Please learn C++!
                        Your TelegramBot class has no constructor without parameter!
                        Also, if you want to allocate TelegramBot on the heap then bot needs to be a pointer. All these are absolute basics.

                        TelegramBot *bot;  
                        bot = new TelegramBot ("my_api_key");
                        

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

                        J 1 Reply Last reply
                        1
                        • 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; }

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

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

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

                          In addition to @jsulm's excellent points. We don't know the scope of your sys_params (probably "global", yuck, don't do that), and:

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

                          We don't know whether you have written anything to the "some fields", or written directly into sys_params.

                          J 1 Reply Last reply
                          0
                          • jsulmJ jsulm

                            @jenya7 This not stack trace...

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

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

                            @jenya7 This not stack trace...

                            1 QString::isEmpty qstring.h 937 0x28bd8
                            2 telebot::telebot telebot.cpp 10 0x56040
                            3 __static_initialization_and_destruction_0 telebot.cpp
                            4 0x5652c 4 _GLOBAL__sub_I_telebot.cpp(void) telebot.cpp 69 0x56588
                            5 __libc_csu_init 0xaf61c
                            6 __libc_start_main libc-start.c 264 0xb5ab96ac
                            7 _start 0x1c5dc

                            1 Reply Last reply
                            0
                            • jsulmJ jsulm

                              @jenya7 Please learn C++!
                              Your TelegramBot class has no constructor without parameter!
                              Also, if you want to allocate TelegramBot on the heap then bot needs to be a pointer. All these are absolute basics.

                              TelegramBot *bot;  
                              bot = new TelegramBot ("my_api_key");
                              
                              J Offline
                              J Offline
                              jenya7
                              wrote on last edited by
                              #27

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

                              @jenya7 Please learn C++!
                              Your TelegramBot class has no constructor without parameter!
                              Also, if you want to allocate TelegramBot on the heap then bot needs to be a pointer. All these are absolute basics.

                              TelegramBot *bot;  
                              bot = new TelegramBot ("my_api_key");
                              

                              Thank you.

                              1 Reply Last reply
                              0
                              • JonBJ JonB

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

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

                                In addition to @jsulm's excellent points. We don't know the scope of your sys_params (probably "global", yuck, don't do that), and:

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

                                We don't know whether you have written anything to the "some fields", or written directly into sys_params.

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

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

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

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

                                In addition to @jsulm's excellent points. We don't know the scope of your sys_params (probably "global", yuck, don't do that), and:

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

                                We don't know whether you have written anything to the "some fields", or written directly into sys_params.

                                If I comment out the constructor stuff - I see all fields of sys_params perfectly initialized. The problem - it gets to
                                bot = new TelegramBot (sys_params.bot_api_key); before it's initialized. The constructor goes first and then goes sys_params initialization (SetupRun(); in main.cpp).

                                Why do I get to the constructor? I don't instantiate any telebot object at all in the code.
                                When I get to the constructor I see
                                sys_params.bot_api_key = <not accessible>
                                how can it be?

                                JonBJ 1 Reply 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:

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

                                  In addition to @jsulm's excellent points. We don't know the scope of your sys_params (probably "global", yuck, don't do that), and:

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

                                  We don't know whether you have written anything to the "some fields", or written directly into sys_params.

                                  If I comment out the constructor stuff - I see all fields of sys_params perfectly initialized. The problem - it gets to
                                  bot = new TelegramBot (sys_params.bot_api_key); before it's initialized. The constructor goes first and then goes sys_params initialization (SetupRun(); in main.cpp).

                                  Why do I get to the constructor? I don't instantiate any telebot object at all in the code.
                                  When I get to the constructor I see
                                  sys_params.bot_api_key = <not accessible>
                                  how can it be?

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

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

                                  I don't instantiate any telebot object at all in the code.

                                  You do if you hit the constructor! And putting a breakpoint there would should you where you do so from.

                                  J 1 Reply Last reply
                                  0
                                  • JonBJ JonB

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

                                    I don't instantiate any telebot object at all in the code.

                                    You do if you hit the constructor! And putting a breakpoint there would should you where you do so from.

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

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

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

                                    I don't instantiate any telebot object at all in the code.

                                    You do if you hit the constructor! And putting a breakpoint there would should you where you do so from.

                                    I see...
                                    In telebot.h I have extern telebot m_telebot;
                                    and in telebot.cpp telebot m_telebot;
                                    in order to work with the object in other parts of the project.
                                    And when I include telebot.h in main.cpp it gets initialized before sys_params is accessible.

                                    JonBJ 1 Reply 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:

                                      I don't instantiate any telebot object at all in the code.

                                      You do if you hit the constructor! And putting a breakpoint there would should you where you do so from.

                                      I see...
                                      In telebot.h I have extern telebot m_telebot;
                                      and in telebot.cpp telebot m_telebot;
                                      in order to work with the object in other parts of the project.
                                      And when I include telebot.h in main.cpp it gets initialized before sys_params is accessible.

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

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

                                      and in telebot.cpp telebot m_telebot;

                                      Indeed, and that is where it will get instantiated.

                                      Since you say it is m_telebot, is it a member variable in some class? Or is it a (mis-named) global? You will not want it to be a truly global variable. If its constructor requires accessing sys_params, and that needs initialising first at run time (e.g. the SetupRun(); you showed), it is your job to ensure sys_params has been initialised before a telebot is created.

                                      J 1 Reply Last reply
                                      2
                                      • JonBJ JonB

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

                                        and in telebot.cpp telebot m_telebot;

                                        Indeed, and that is where it will get instantiated.

                                        Since you say it is m_telebot, is it a member variable in some class? Or is it a (mis-named) global? You will not want it to be a truly global variable. If its constructor requires accessing sys_params, and that needs initialising first at run time (e.g. the SetupRun(); you showed), it is your job to ensure sys_params has been initialised before a telebot is created.

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

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

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

                                        and in telebot.cpp telebot m_telebot;

                                        Indeed, and that is where it will get instantiated.

                                        Since you say it is m_telebot, is it a member variable in some class? Or is it a (mis-named) global? You will not want it to be a truly global variable. If its constructor requires accessing sys_params, and that needs initialising first at run time (e.g. the SetupRun(); you showed), it is your job to ensure sys_params has been initialised before a telebot is created.

                                        I did it this way

                                        telebot m_telebot;
                                        
                                        static TelegramBot *bot = nullptr;
                                        
                                        telebot::telebot(QObject *parent) : QObject(parent)
                                        {
                                             //empty constructor 
                                        }
                                        
                                        void telebot::Start(QString api_key)
                                        {
                                            bot = new TelegramBot(api_key);
                                        
                                            QObject::connect(bot, &TelegramBot::newMessage, this, &telebot::NewMessage);
                                        
                                            bot->startMessagePulling();
                                        }
                                        

                                        and in main.cpp

                                        int main(int argc, char *argv[])
                                        {
                                            QApplication a(argc, argv);
                                        
                                             MainWindow w;
                                             w.show();
                                        
                                             SetupRun();
                                        
                                            m_telebot.Start(sys_params.bot_api_key);
                                            m_telebot.SendText(sys_params.bot_chat_id, "Control board is connected");
                                        }
                                        

                                        This way it works good.

                                        JonBJ jsulmJ 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:

                                          and in telebot.cpp telebot m_telebot;

                                          Indeed, and that is where it will get instantiated.

                                          Since you say it is m_telebot, is it a member variable in some class? Or is it a (mis-named) global? You will not want it to be a truly global variable. If its constructor requires accessing sys_params, and that needs initialising first at run time (e.g. the SetupRun(); you showed), it is your job to ensure sys_params has been initialised before a telebot is created.

                                          I did it this way

                                          telebot m_telebot;
                                          
                                          static TelegramBot *bot = nullptr;
                                          
                                          telebot::telebot(QObject *parent) : QObject(parent)
                                          {
                                               //empty constructor 
                                          }
                                          
                                          void telebot::Start(QString api_key)
                                          {
                                              bot = new TelegramBot(api_key);
                                          
                                              QObject::connect(bot, &TelegramBot::newMessage, this, &telebot::NewMessage);
                                          
                                              bot->startMessagePulling();
                                          }
                                          

                                          and in main.cpp

                                          int main(int argc, char *argv[])
                                          {
                                              QApplication a(argc, argv);
                                          
                                               MainWindow w;
                                               w.show();
                                          
                                               SetupRun();
                                          
                                              m_telebot.Start(sys_params.bot_api_key);
                                              m_telebot.SendText(sys_params.bot_chat_id, "Control board is connected");
                                          }
                                          

                                          This way it works good.

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

                                          @jenya7
                                          I am happy this is working for you. I will just say that you really ought not have your global telebot and TelegramBot * variables, things would be better if you used classes and instances in C++. But that's up to you. So long as telebot m_telebot is global you really ought not have that m_ prefix on the name, that is conventionally used (by some) for class member variables and will lead to confusion for anyone looking at your code, as I suspected.

                                          J 1 Reply Last reply
                                          1

                                          • Login

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