How to get Message object from MessageService object in Telethon
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
add a comment |
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
add a comment |
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
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
telegram telethon
asked Nov 25 '18 at 13:31
AntonAnton
136
136
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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', '...'))
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 anInputChannelobject manually. This is what telethon does under the hood for public channels when you provide@username.InputChannelhas 2 mandatory arguments:idandaccess_hash. You may callGetAllChatsRequestto 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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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', '...'))
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 anInputChannelobject manually. This is what telethon does under the hood for public channels when you provide@username.InputChannelhas 2 mandatory arguments:idandaccess_hash. You may callGetAllChatsRequestto 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
add a comment |
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', '...'))
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 anInputChannelobject manually. This is what telethon does under the hood for public channels when you provide@username.InputChannelhas 2 mandatory arguments:idandaccess_hash. You may callGetAllChatsRequestto 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
add a comment |
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', '...'))
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', '...'))
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 anInputChannelobject manually. This is what telethon does under the hood for public channels when you provide@username.InputChannelhas 2 mandatory arguments:idandaccess_hash. You may callGetAllChatsRequestto 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
add a comment |
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 anInputChannelobject manually. This is what telethon does under the hood for public channels when you provide@username.InputChannelhas 2 mandatory arguments:idandaccess_hash. You may callGetAllChatsRequestto 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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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