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.8k 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.
  • 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 Offline
      jsulmJ Offline
      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
                        • 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.

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

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

                          This way it works good

                          But is really bad design as @JonB pointed out. Global variables should be avoided as much as possible...

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

                          J 1 Reply Last reply
                          0
                          • JonBJ JonB

                            @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 Offline
                            J Offline
                            jenya7
                            wrote on last edited by
                            #35

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

                            @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.

                            How should I use a telebot class in other modules without having it global?

                            1 Reply Last reply
                            0
                            • jsulmJ jsulm

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

                              This way it works good

                              But is really bad design as @JonB pointed out. Global variables should be avoided as much as possible...

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

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

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

                              This way it works good

                              But is really bad design as @JonB pointed out. Global variables should be avoided as much as possible...

                              I want to send messages

                              m_telebot.SendText(sys_params.bot_chat_id, "My messaged");
                              

                              from other modules. how is it possible if m_telebot is not global?

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

                                This way it works good

                                But is really bad design as @JonB pointed out. Global variables should be avoided as much as possible...

                                I want to send messages

                                m_telebot.SendText(sys_params.bot_chat_id, "My messaged");
                                

                                from other modules. how is it possible if m_telebot is not global?

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

                                @jenya7
                                I feared that you might ask this! I'm afraid I am not offering to teach/illustrate general C++ principles :) There are various alternatives, like singleton classes, even static class methods/variables, putting them into application state, doing it like QSqlDatabase does, .... I imagine that if you Google there are various suggestions proposed. I was just saying that nowadays having bald C-style global variables is "frowned upon. But also as I said, up to you.

                                I will make one comment. So long as this telebot m_telebot; is in your main.cpp, how do you intend to access it in other files/modules/classes anyway? I hope you're not intending to #include "main.h" in all your other .cpp files?

                                J 1 Reply Last reply
                                0
                                • JonBJ JonB

                                  @jenya7
                                  I feared that you might ask this! I'm afraid I am not offering to teach/illustrate general C++ principles :) There are various alternatives, like singleton classes, even static class methods/variables, putting them into application state, doing it like QSqlDatabase does, .... I imagine that if you Google there are various suggestions proposed. I was just saying that nowadays having bald C-style global variables is "frowned upon. But also as I said, up to you.

                                  I will make one comment. So long as this telebot m_telebot; is in your main.cpp, how do you intend to access it in other files/modules/classes anyway? I hope you're not intending to #include "main.h" in all your other .cpp files?

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

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

                                  @jenya7
                                  I feared that you might ask this! I'm afraid I am not offering to teach/illustrate general C++ principles :) There are various alternatives, like singleton classes, even static class methods/variables, putting them into application state, doing it like QSqlDatabase does, .... I imagine that if you Google there are various suggestions proposed. I was just saying that nowadays having bald C-style global variables is "frowned upon. But also as I said, up to you.

                                  I will make one comment. So long as this telebot m_telebot; is in your main.cpp, how do you intend to access it in other files/modules/classes anyway? I hope you're not intending to #include "main.h" in all your other .cpp files?

                                  extern telebot m_telebot; works pretty well.

                                  JonBJ 1 Reply Last reply
                                  0
                                  • J jenya7

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

                                    @jenya7
                                    I feared that you might ask this! I'm afraid I am not offering to teach/illustrate general C++ principles :) There are various alternatives, like singleton classes, even static class methods/variables, putting them into application state, doing it like QSqlDatabase does, .... I imagine that if you Google there are various suggestions proposed. I was just saying that nowadays having bald C-style global variables is "frowned upon. But also as I said, up to you.

                                    I will make one comment. So long as this telebot m_telebot; is in your main.cpp, how do you intend to access it in other files/modules/classes anyway? I hope you're not intending to #include "main.h" in all your other .cpp files?

                                    extern telebot m_telebot; works pretty well.

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

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

                                    extern telebot m_telebot; works pretty well.

                                    Until that module links with something which does not define a global telebot m_telebot, and then it falls over. Every module you write relies on your main.cpp defining it, not good. Again, if you want to write code where modules use extern to reference something without getting it from the appropriate header file that is up to you.

                                    J 1 Reply Last reply
                                    0
                                    • JonBJ JonB

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

                                      extern telebot m_telebot; works pretty well.

                                      Until that module links with something which does not define a global telebot m_telebot, and then it falls over. Every module you write relies on your main.cpp defining it, not good. Again, if you want to write code where modules use extern to reference something without getting it from the appropriate header file that is up to you.

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

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

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

                                      extern telebot m_telebot; works pretty well.

                                      Until that module links with something which does not define a global telebot m_telebot, and then it falls over. Every module you write relies on your main.cpp defining it, not good. Again, if you want to write code where modules use extern to reference something without getting it from the appropriate header file that is up to you.

                                      I'm loosing you.

                                      module.h

                                      extern MY_CLASS m_my_class;
                                      

                                      module.cpp

                                      MY_CLASS m_my_class;
                                      

                                      another_module.cpp

                                      #include "module.h"
                                      
                                      m_my_class.Method_1();
                                      

                                      How do you think a static class works? Not the same way?

                                      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:

                                        extern telebot m_telebot; works pretty well.

                                        Until that module links with something which does not define a global telebot m_telebot, and then it falls over. Every module you write relies on your main.cpp defining it, not good. Again, if you want to write code where modules use extern to reference something without getting it from the appropriate header file that is up to you.

                                        I'm loosing you.

                                        module.h

                                        extern MY_CLASS m_my_class;
                                        

                                        module.cpp

                                        MY_CLASS m_my_class;
                                        

                                        another_module.cpp

                                        #include "module.h"
                                        
                                        m_my_class.Method_1();
                                        

                                        How do you think a static class works? Not the same way?

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

                                        @jenya7
                                        This is "fine", insofar as you have changed the example to some other module.cpp/.h. But your code for telebot m_telebot; I thought was in main.cpp, and that would require including main.h elsewhere. I now think did not mean it was in main, that was not clear to me.

                                        While you choose to use a global variable, you already previously discovered (with your sys_params and TelegramBot ) that you have no opportunity to do anything initialisation-wise prior to its construction, which your code came a cropper on, you didn't understand and you had to work around. Same could happen with destructor. (You may or may not be aware, but additionally you can have a QString member (like your SYS_PARAMS sys_params) but you cannot have any QObject-derived members for such a class if you wanted them.) But it didn't put you off, so up to you.

                                        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