How to get Message object from MessageService object in Telethon












1















I'm getting TypeError: 'MessageService' object is not iterable



Fist I'm saving last 10 messages from a channel using iter_messages client method which returns telethon.sync._SyncGen generator object.
Then I'm iterating over this generator and trying send each message (msg) to the user (username) through client's send_message method which can take either str or telethon Message object as a message argument.



However my msg object here is not an instance of the Message class but MessageService class (https://lonamiwebs.github.io/Telethon/constructors/message_service.html) and I assume this is the reason I'm getting the error.



message_objects = client.iter_messages(channel_name, limit=10)

for msg in message_objects:
client.send_message(username, msg)


My question is how can get Message objects instead of MessageService in order to avoid the error and make the client.send_message() work properly?










share|improve this question



























    1















    I'm getting TypeError: 'MessageService' object is not iterable



    Fist I'm saving last 10 messages from a channel using iter_messages client method which returns telethon.sync._SyncGen generator object.
    Then I'm iterating over this generator and trying send each message (msg) to the user (username) through client's send_message method which can take either str or telethon Message object as a message argument.



    However my msg object here is not an instance of the Message class but MessageService class (https://lonamiwebs.github.io/Telethon/constructors/message_service.html) and I assume this is the reason I'm getting the error.



    message_objects = client.iter_messages(channel_name, limit=10)

    for msg in message_objects:
    client.send_message(username, msg)


    My question is how can get Message objects instead of MessageService in order to avoid the error and make the client.send_message() work properly?










    share|improve this question

























      1












      1








      1








      I'm getting TypeError: 'MessageService' object is not iterable



      Fist I'm saving last 10 messages from a channel using iter_messages client method which returns telethon.sync._SyncGen generator object.
      Then I'm iterating over this generator and trying send each message (msg) to the user (username) through client's send_message method which can take either str or telethon Message object as a message argument.



      However my msg object here is not an instance of the Message class but MessageService class (https://lonamiwebs.github.io/Telethon/constructors/message_service.html) and I assume this is the reason I'm getting the error.



      message_objects = client.iter_messages(channel_name, limit=10)

      for msg in message_objects:
      client.send_message(username, msg)


      My question is how can get Message objects instead of MessageService in order to avoid the error and make the client.send_message() work properly?










      share|improve this question














      I'm getting TypeError: 'MessageService' object is not iterable



      Fist I'm saving last 10 messages from a channel using iter_messages client method which returns telethon.sync._SyncGen generator object.
      Then I'm iterating over this generator and trying send each message (msg) to the user (username) through client's send_message method which can take either str or telethon Message object as a message argument.



      However my msg object here is not an instance of the Message class but MessageService class (https://lonamiwebs.github.io/Telethon/constructors/message_service.html) and I assume this is the reason I'm getting the error.



      message_objects = client.iter_messages(channel_name, limit=10)

      for msg in message_objects:
      client.send_message(username, msg)


      My question is how can get Message objects instead of MessageService in order to avoid the error and make the client.send_message() work properly?







      telegram telethon






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 25 '18 at 13:31









      AntonAnton

      136




      136
























          1 Answer
          1






          active

          oldest

          votes


















          1














          MessageService objects are messages by Telegram e.g. "somebody joined this group" or "channel photo changed". iter_messages returns these messages along with other messages but you cannot send these messages. As you can see in the documentation you linked yourself, there is no real message inside a MessageService object. There is only a MessageAction.



          You can skip this type of messages in your loop my checking their type() or by hasattr(msg, 'message'). Normal messages have message field which is the text you want to send. If you want to send_message (not forward), I think your code should be changed to:



          client.send_message(username, getattr(msg, 'message', '...'))





          share|improve this answer



















          • 1





            Thank you! That's exactly what I was looking for. And if I need to send or forward these messages to a private channel, what is the best way to do it?

            – Anton
            Nov 26 '18 at 9:44








          • 1





            For private channels, you should instantiate an InputChannel object manually. This is what telethon does under the hood for public channels when you provide @username. InputChannel has 2 mandatory arguments: id and access_hash. You may call GetAllChatsRequest to get a full list of all chats of your account and find the specific private channel among them.

            – Ali Hashemi
            Nov 26 '18 at 11:21











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53467988%2fhow-to-get-message-object-from-messageservice-object-in-telethon%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          MessageService objects are messages by Telegram e.g. "somebody joined this group" or "channel photo changed". iter_messages returns these messages along with other messages but you cannot send these messages. As you can see in the documentation you linked yourself, there is no real message inside a MessageService object. There is only a MessageAction.



          You can skip this type of messages in your loop my checking their type() or by hasattr(msg, 'message'). Normal messages have message field which is the text you want to send. If you want to send_message (not forward), I think your code should be changed to:



          client.send_message(username, getattr(msg, 'message', '...'))





          share|improve this answer



















          • 1





            Thank you! That's exactly what I was looking for. And if I need to send or forward these messages to a private channel, what is the best way to do it?

            – Anton
            Nov 26 '18 at 9:44








          • 1





            For private channels, you should instantiate an InputChannel object manually. This is what telethon does under the hood for public channels when you provide @username. InputChannel has 2 mandatory arguments: id and access_hash. You may call GetAllChatsRequest to get a full list of all chats of your account and find the specific private channel among them.

            – Ali Hashemi
            Nov 26 '18 at 11:21
















          1














          MessageService objects are messages by Telegram e.g. "somebody joined this group" or "channel photo changed". iter_messages returns these messages along with other messages but you cannot send these messages. As you can see in the documentation you linked yourself, there is no real message inside a MessageService object. There is only a MessageAction.



          You can skip this type of messages in your loop my checking their type() or by hasattr(msg, 'message'). Normal messages have message field which is the text you want to send. If you want to send_message (not forward), I think your code should be changed to:



          client.send_message(username, getattr(msg, 'message', '...'))





          share|improve this answer



















          • 1





            Thank you! That's exactly what I was looking for. And if I need to send or forward these messages to a private channel, what is the best way to do it?

            – Anton
            Nov 26 '18 at 9:44








          • 1





            For private channels, you should instantiate an InputChannel object manually. This is what telethon does under the hood for public channels when you provide @username. InputChannel has 2 mandatory arguments: id and access_hash. You may call GetAllChatsRequest to get a full list of all chats of your account and find the specific private channel among them.

            – Ali Hashemi
            Nov 26 '18 at 11:21














          1












          1








          1







          MessageService objects are messages by Telegram e.g. "somebody joined this group" or "channel photo changed". iter_messages returns these messages along with other messages but you cannot send these messages. As you can see in the documentation you linked yourself, there is no real message inside a MessageService object. There is only a MessageAction.



          You can skip this type of messages in your loop my checking their type() or by hasattr(msg, 'message'). Normal messages have message field which is the text you want to send. If you want to send_message (not forward), I think your code should be changed to:



          client.send_message(username, getattr(msg, 'message', '...'))





          share|improve this answer













          MessageService objects are messages by Telegram e.g. "somebody joined this group" or "channel photo changed". iter_messages returns these messages along with other messages but you cannot send these messages. As you can see in the documentation you linked yourself, there is no real message inside a MessageService object. There is only a MessageAction.



          You can skip this type of messages in your loop my checking their type() or by hasattr(msg, 'message'). Normal messages have message field which is the text you want to send. If you want to send_message (not forward), I think your code should be changed to:



          client.send_message(username, getattr(msg, 'message', '...'))






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 26 '18 at 4:44









          Ali HashemiAli Hashemi

          1,55432435




          1,55432435








          • 1





            Thank you! That's exactly what I was looking for. And if I need to send or forward these messages to a private channel, what is the best way to do it?

            – Anton
            Nov 26 '18 at 9:44








          • 1





            For private channels, you should instantiate an InputChannel object manually. This is what telethon does under the hood for public channels when you provide @username. InputChannel has 2 mandatory arguments: id and access_hash. You may call GetAllChatsRequest to get a full list of all chats of your account and find the specific private channel among them.

            – Ali Hashemi
            Nov 26 '18 at 11:21














          • 1





            Thank you! That's exactly what I was looking for. And if I need to send or forward these messages to a private channel, what is the best way to do it?

            – Anton
            Nov 26 '18 at 9:44








          • 1





            For private channels, you should instantiate an InputChannel object manually. This is what telethon does under the hood for public channels when you provide @username. InputChannel has 2 mandatory arguments: id and access_hash. You may call GetAllChatsRequest to get a full list of all chats of your account and find the specific private channel among them.

            – Ali Hashemi
            Nov 26 '18 at 11:21








          1




          1





          Thank you! That's exactly what I was looking for. And if I need to send or forward these messages to a private channel, what is the best way to do it?

          – Anton
          Nov 26 '18 at 9:44







          Thank you! That's exactly what I was looking for. And if I need to send or forward these messages to a private channel, what is the best way to do it?

          – Anton
          Nov 26 '18 at 9:44






          1




          1





          For private channels, you should instantiate an InputChannel object manually. This is what telethon does under the hood for public channels when you provide @username. InputChannel has 2 mandatory arguments: id and access_hash. You may call GetAllChatsRequest to get a full list of all chats of your account and find the specific private channel among them.

          – Ali Hashemi
          Nov 26 '18 at 11:21





          For private channels, you should instantiate an InputChannel object manually. This is what telethon does under the hood for public channels when you provide @username. InputChannel has 2 mandatory arguments: id and access_hash. You may call GetAllChatsRequest to get a full list of all chats of your account and find the specific private channel among them.

          – Ali Hashemi
          Nov 26 '18 at 11:21


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53467988%2fhow-to-get-message-object-from-messageservice-object-in-telethon%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Contact image not getting when fetch all contact list from iPhone by CNContact

          count number of partitions of a set with n elements into k subsets

          A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks