forward message to super group with telethon












1















Recently I wrote code that should forward every message from a certain user to all groups that I joined but it doesn't.
Here my code:



        for message in client.iter_messages('aliakhtari78'):
try:
dialogs = client.get_dialogs()
for dialog in dialogs:
id_chat = dialog.message.to_id.channel_id
entity = client.get_entity(id_chat)
client.forward_messages(
entity, # to which entity you are forwarding the messages
message.id, # the IDs of the messages (or message) to forward
'somebody' # who sent the messages?
)

except:
pass


in this code first I take every message which send to me by 'aliakhtari78' and then get entity of the groups that I joined to and in the end it should forward the message to all groups but it doesn't, I check my code and replace the entity with a user entity and it worked and I know the problem is because of entity, but I cant find out what is my problem.
In addition, I'm sorry for writing mistakes in my question.










share|improve this question



























    1















    Recently I wrote code that should forward every message from a certain user to all groups that I joined but it doesn't.
    Here my code:



            for message in client.iter_messages('aliakhtari78'):
    try:
    dialogs = client.get_dialogs()
    for dialog in dialogs:
    id_chat = dialog.message.to_id.channel_id
    entity = client.get_entity(id_chat)
    client.forward_messages(
    entity, # to which entity you are forwarding the messages
    message.id, # the IDs of the messages (or message) to forward
    'somebody' # who sent the messages?
    )

    except:
    pass


    in this code first I take every message which send to me by 'aliakhtari78' and then get entity of the groups that I joined to and in the end it should forward the message to all groups but it doesn't, I check my code and replace the entity with a user entity and it worked and I know the problem is because of entity, but I cant find out what is my problem.
    In addition, I'm sorry for writing mistakes in my question.










    share|improve this question

























      1












      1








      1


      3






      Recently I wrote code that should forward every message from a certain user to all groups that I joined but it doesn't.
      Here my code:



              for message in client.iter_messages('aliakhtari78'):
      try:
      dialogs = client.get_dialogs()
      for dialog in dialogs:
      id_chat = dialog.message.to_id.channel_id
      entity = client.get_entity(id_chat)
      client.forward_messages(
      entity, # to which entity you are forwarding the messages
      message.id, # the IDs of the messages (or message) to forward
      'somebody' # who sent the messages?
      )

      except:
      pass


      in this code first I take every message which send to me by 'aliakhtari78' and then get entity of the groups that I joined to and in the end it should forward the message to all groups but it doesn't, I check my code and replace the entity with a user entity and it worked and I know the problem is because of entity, but I cant find out what is my problem.
      In addition, I'm sorry for writing mistakes in my question.










      share|improve this question














      Recently I wrote code that should forward every message from a certain user to all groups that I joined but it doesn't.
      Here my code:



              for message in client.iter_messages('aliakhtari78'):
      try:
      dialogs = client.get_dialogs()
      for dialog in dialogs:
      id_chat = dialog.message.to_id.channel_id
      entity = client.get_entity(id_chat)
      client.forward_messages(
      entity, # to which entity you are forwarding the messages
      message.id, # the IDs of the messages (or message) to forward
      'somebody' # who sent the messages?
      )

      except:
      pass


      in this code first I take every message which send to me by 'aliakhtari78' and then get entity of the groups that I joined to and in the end it should forward the message to all groups but it doesn't, I check my code and replace the entity with a user entity and it worked and I know the problem is because of entity, but I cant find out what is my problem.
      In addition, I'm sorry for writing mistakes in my question.







      python python-3.x telegram python-telegram-bot telethon






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 16 '18 at 22:27









      AliAli

      196112




      196112
























          1 Answer
          1






          active

          oldest

          votes


















          3














          In order to send messages to any entities in Telegram, you need two pieces of information:




          1. the constant unique ID of the entity (It's an integer. It's NOT username string)

          2. the access_hash which is different for each user for each entity


          You can only pass @username to client.get_entity, and Telethon automatically resolves the @username to an entity with id and access_hash. That's why it works when you change your code like that. However, in your code, you have passed channel_id (which is the constant unique ID of the entity) to client.get_entity, not username.



          Note that client.get_dialogs returns entities along with dialogs. You have just ignored the entities! This is how you can get an array of all entities:



          dialogs, entities = client.get_dialogs()


          Then simply pass the corresponding entity from the entities array to client.forward_messages.






          share|improve this answer





















          • 1





            I tried your code but it doesn't work either. here my code, as you see the line of code that you write isn't work. link and with out try and except I get this error : "dialogs, entities = client.get_dialogs() ValueError: too many values to unpack (expected 2)"

            – Ali
            Nov 22 '18 at 18:50








          • 1





            I find out my problem now, in my full code I used try except 2 time in my scope, and when I removed that my code worked properly.

            – Ali
            Nov 22 '18 at 18:59











          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%2f53346239%2fforward-message-to-super-group-with-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









          3














          In order to send messages to any entities in Telegram, you need two pieces of information:




          1. the constant unique ID of the entity (It's an integer. It's NOT username string)

          2. the access_hash which is different for each user for each entity


          You can only pass @username to client.get_entity, and Telethon automatically resolves the @username to an entity with id and access_hash. That's why it works when you change your code like that. However, in your code, you have passed channel_id (which is the constant unique ID of the entity) to client.get_entity, not username.



          Note that client.get_dialogs returns entities along with dialogs. You have just ignored the entities! This is how you can get an array of all entities:



          dialogs, entities = client.get_dialogs()


          Then simply pass the corresponding entity from the entities array to client.forward_messages.






          share|improve this answer





















          • 1





            I tried your code but it doesn't work either. here my code, as you see the line of code that you write isn't work. link and with out try and except I get this error : "dialogs, entities = client.get_dialogs() ValueError: too many values to unpack (expected 2)"

            – Ali
            Nov 22 '18 at 18:50








          • 1





            I find out my problem now, in my full code I used try except 2 time in my scope, and when I removed that my code worked properly.

            – Ali
            Nov 22 '18 at 18:59
















          3














          In order to send messages to any entities in Telegram, you need two pieces of information:




          1. the constant unique ID of the entity (It's an integer. It's NOT username string)

          2. the access_hash which is different for each user for each entity


          You can only pass @username to client.get_entity, and Telethon automatically resolves the @username to an entity with id and access_hash. That's why it works when you change your code like that. However, in your code, you have passed channel_id (which is the constant unique ID of the entity) to client.get_entity, not username.



          Note that client.get_dialogs returns entities along with dialogs. You have just ignored the entities! This is how you can get an array of all entities:



          dialogs, entities = client.get_dialogs()


          Then simply pass the corresponding entity from the entities array to client.forward_messages.






          share|improve this answer





















          • 1





            I tried your code but it doesn't work either. here my code, as you see the line of code that you write isn't work. link and with out try and except I get this error : "dialogs, entities = client.get_dialogs() ValueError: too many values to unpack (expected 2)"

            – Ali
            Nov 22 '18 at 18:50








          • 1





            I find out my problem now, in my full code I used try except 2 time in my scope, and when I removed that my code worked properly.

            – Ali
            Nov 22 '18 at 18:59














          3












          3








          3







          In order to send messages to any entities in Telegram, you need two pieces of information:




          1. the constant unique ID of the entity (It's an integer. It's NOT username string)

          2. the access_hash which is different for each user for each entity


          You can only pass @username to client.get_entity, and Telethon automatically resolves the @username to an entity with id and access_hash. That's why it works when you change your code like that. However, in your code, you have passed channel_id (which is the constant unique ID of the entity) to client.get_entity, not username.



          Note that client.get_dialogs returns entities along with dialogs. You have just ignored the entities! This is how you can get an array of all entities:



          dialogs, entities = client.get_dialogs()


          Then simply pass the corresponding entity from the entities array to client.forward_messages.






          share|improve this answer















          In order to send messages to any entities in Telegram, you need two pieces of information:




          1. the constant unique ID of the entity (It's an integer. It's NOT username string)

          2. the access_hash which is different for each user for each entity


          You can only pass @username to client.get_entity, and Telethon automatically resolves the @username to an entity with id and access_hash. That's why it works when you change your code like that. However, in your code, you have passed channel_id (which is the constant unique ID of the entity) to client.get_entity, not username.



          Note that client.get_dialogs returns entities along with dialogs. You have just ignored the entities! This is how you can get an array of all entities:



          dialogs, entities = client.get_dialogs()


          Then simply pass the corresponding entity from the entities array to client.forward_messages.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 20 '18 at 13:02

























          answered Nov 20 '18 at 12:55









          Ali HashemiAli Hashemi

          1,53432335




          1,53432335








          • 1





            I tried your code but it doesn't work either. here my code, as you see the line of code that you write isn't work. link and with out try and except I get this error : "dialogs, entities = client.get_dialogs() ValueError: too many values to unpack (expected 2)"

            – Ali
            Nov 22 '18 at 18:50








          • 1





            I find out my problem now, in my full code I used try except 2 time in my scope, and when I removed that my code worked properly.

            – Ali
            Nov 22 '18 at 18:59














          • 1





            I tried your code but it doesn't work either. here my code, as you see the line of code that you write isn't work. link and with out try and except I get this error : "dialogs, entities = client.get_dialogs() ValueError: too many values to unpack (expected 2)"

            – Ali
            Nov 22 '18 at 18:50








          • 1





            I find out my problem now, in my full code I used try except 2 time in my scope, and when I removed that my code worked properly.

            – Ali
            Nov 22 '18 at 18:59








          1




          1





          I tried your code but it doesn't work either. here my code, as you see the line of code that you write isn't work. link and with out try and except I get this error : "dialogs, entities = client.get_dialogs() ValueError: too many values to unpack (expected 2)"

          – Ali
          Nov 22 '18 at 18:50







          I tried your code but it doesn't work either. here my code, as you see the line of code that you write isn't work. link and with out try and except I get this error : "dialogs, entities = client.get_dialogs() ValueError: too many values to unpack (expected 2)"

          – Ali
          Nov 22 '18 at 18:50






          1




          1





          I find out my problem now, in my full code I used try except 2 time in my scope, and when I removed that my code worked properly.

          – Ali
          Nov 22 '18 at 18:59





          I find out my problem now, in my full code I used try except 2 time in my scope, and when I removed that my code worked properly.

          – Ali
          Nov 22 '18 at 18:59


















          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%2f53346239%2fforward-message-to-super-group-with-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

          Guess what letter conforming each word

          Port of Spain

          Run scheduled task as local user group (not BUILTIN)