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.2k 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 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