Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't forward messages to anyone but myself #3

Closed
IvanMazzoli opened this issue Jan 15, 2018 · 12 comments
Closed

Can't forward messages to anyone but myself #3

IvanMazzoli opened this issue Jan 15, 2018 · 12 comments
Labels

Comments

@IvanMazzoli
Copy link

IvanMazzoli commented Jan 15, 2018

I'm trying to forward a message to a group but it doesn't allow me to do it.
The only chat I'm able to forward messages is my own chat (saved messages).

What I've tried:
client.forward_messages(-100129xxxxxxx, chat_id, msg_ids) to a group, doesn't work.
client.forward_messages(446xxx, chat_id, msg_ids) to my friend, doesn't work.
client.forward_messages("@myfriendusername", chat_id, msg_ids) to my friend, doesn't work. Same without the @.

The log file shows this error (it's similare between al cases, changes only the KeyError):

INFO:pyrogram.connection.connection:Connecting...
INFO:pyrogram.connection.transport.tcp.tcp_abridged:Connected!
INFO:pyrogram.session.session:Connection inited: Layer 74
INFO:pyrogram.client.client:Dialogs count: 12
ERROR:pyrogram.session.session:[400 PEER_ID_INVALID]: The id/access_hash combination is invalid
Traceback (most recent call last):
  File "/home/dreadtank27/.local/lib/python3.5/site-packages/pyrogram/client/client.py", line 441, in resolve_peer
    else self.peers_by_id[chat_id]
KeyError: -100129xxxxxxx

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/dreadtank27/.local/lib/python3.5/site-packages/pyrogram/session/session.py", line 237, in worker
    self.unpack_dispatch_and_ack(packet)
  File "/home/dreadtank27/.local/lib/python3.5/site-packages/pyrogram/session/session.py", line 284, in unpack_dispatch_and_ack
    self.update_handler(i.body)
  File "channels_forwarder.py", line 12, in callback
    client.forward_messages(-100129xxxxxxx, chat_id, msg_ids)
  File "/home/dreadtank27/.local/lib/python3.5/site-packages/pyrogram/client/client.py", line 538, in forward_messages
    to_peer=self.resolve_peer(chat_id),
  File "/home/dreadtank27/.local/lib/python3.5/site-packages/pyrogram/client/client.py", line 444, in resolve_peer
    raise PeerIdInvalid
pyrogram.api.errors.exceptions.bad_request_400.PeerIdInvalid: [400 PEER_ID_INVALID]: The id/access_hash combination is invalid

Also, I'm an administrator in the destination group, my friend hasn't blocked me and the key/access_hash is correct.
I'm using Pyrogram V.0.3.3

@delivrance
Copy link
Member

@IvanMazzoli About client.forward_messages(-100129xxxxxxx, chat_id, msg_ids): -100129xxxxxxx is the ID the Bot API is using when referring to channels/supergroups, but in Pyrogram (at the moment) you must remove the -100, so your channel/supergroup IDs should look like: 129xxxxxxx.

Regarding the other two (but probably applies to the first one too): Does chat_id contain the ID of an entity (user/group/channel) you have already contacted/joined before? You won't be able (for now) to forward messages from/to entities you don't have in your dialogs list.

@IvanMazzoli
Copy link
Author

Thanks @delivrance, removing -100 did the job!
For the other two, chat_id contains in the first case the chat ID of the user (obtained from a "normal" bot, could be it the cause of the problem?), in the second case the public ID of my friend.
I have indeed already talked to this person, I also have his phone number.

@delivrance
Copy link
Member

Users IDs taken from bots are fine, as soon as you have a conversation with them in your dialogs list. Can you post the full traceback of both calls separately?

@IvanMazzoli
Copy link
Author

IvanMazzoli commented Jan 15, 2018

Sure, but now to be sure I've talked with my own other Telegram account and replaced chat_id and public ID.
If you see different variables in my code, don't worry because it works by forwarding to a supergroup ;)

  • Trying with chat_id:
ERROR:pyrogram.session.session:[400 PEER_ID_INVALID]: The id/access_hash combination is invalid
Traceback (most recent call last):
  File "/home/dreadtank27/.local/lib/python3.5/site-packages/pyrogram/client/client.py", line 441, in resolve_peer
    else self.peers_by_id[chat_id]
KeyError: 2977xxxx

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/dreadtank27/.local/lib/python3.5/site-packages/pyrogram/session/session.py", line 237, in worker
    self.unpack_dispatch_and_ack(packet)
  File "/home/dreadtank27/.local/lib/python3.5/site-packages/pyrogram/session/session.py", line 284, in unpack_dispatch_and_ack
    self.update_handler(i.body)
  File "test.py", line 13, in callback
    client.forward_messages(2977xxxx, upd.message.to_id.channel_id, ids)
  File "/home/dreadtank27/.local/lib/python3.5/site-packages/pyrogram/client/client.py", line 538, in forward_messages
    to_peer=self.resolve_peer(chat_id),
  File "/home/dreadtank27/.local/lib/python3.5/site-packages/pyrogram/client/client.py", line 444, in resolve_peer
    raise PeerIdInvalid
pyrogram.api.errors.exceptions.bad_request_400.PeerIdInvalid: [400 PEER_ID_INVALID]: The id/access_hash combination is invalid
  • Using public ID:
ERROR:pyrogram.session.session:[400 PEER_ID_INVALID]: The id/access_hash combination is invalid
Traceback (most recent call last):
  File "/home/dreadtank27/.local/lib/python3.5/site-packages/pyrogram/client/client.py", line 440, in resolve_peer
    if isinstance(chat_id, str)
KeyError: 'il******ivan'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/dreadtank27/.local/lib/python3.5/site-packages/pyrogram/session/session.py", line 237, in worker
    self.unpack_dispatch_and_ack(packet)
  File "/home/dreadtank27/.local/lib/python3.5/site-packages/pyrogram/session/session.py", line 284, in unpack_dispatch_and_ack
    self.update_handler(i.body)
  File "test.py", line 13, in callback
    client.forward_messages("Il******Ivan", upd.message.to_id.channel_id, ids)
  File "/home/dreadtank27/.local/lib/python3.5/site-packages/pyrogram/client/client.py", line 538, in forward_messages
    to_peer=self.resolve_peer(chat_id),
  File "/home/dreadtank27/.local/lib/python3.5/site-packages/pyrogram/client/client.py", line 444, in resolve_peer
    raise PeerIdInvalid
pyrogram.api.errors.exceptions.bad_request_400.PeerIdInvalid: [400 PEER_ID_INVALID]: The id/access_hash combination is invalid
INFO:pyrogram.session.session:Send 8 acks

@delivrance
Copy link
Member

Please post your code, I'll try to replicate this issue tomorrow.

@IvanMazzoli
Copy link
Author

The basic idea of the script is to forward messages from channels to a specific group, here's the full source code.

import logging
from pyrogram import Client
from pyrogram.api import types

def callback(update):
	if isinstance(update, types.Update):
		for upd in update.updates:
			if isinstance(upd, types.UpdateNewChannelMessage) and upd.message.post:
				print('>> New channel message!')
				logging.basicConfig(filename='log.txt',level=logging.DEBUG)
				ids = [upd.message.id]
				client.forward_messages("@myfriendusername", upd.message.to_id.channel_id, ids)
				logging.info('###################################')
				logging.info(str(upd))
				
client = Client(session_name="tdi")
client.set_update_handler(callback)

client.start()
client.idle()

@delivrance
Copy link
Member

delivrance commented Jan 15, 2018

I've pushed a change that might fix this. Try again your code after you upgrade using pip install --upgrade git+https://github.com/pyrogram/pyrogram

You can also do this manually as explained here: https://docs.pyrogram.ml/getting_started/QuickInstallation/#bleeding-edge

@IvanMazzoli
Copy link
Author

IvanMazzoli commented Jan 15, 2018

Okay, now using @username works as intended. Unforunately, using a chat_id like 2977xxxx doesn't work.

ERROR:pyrogram.session.session:[400 PEER_ID_INVALID]: The id/access_hash combination is invalid
Traceback (most recent call last):
  File "/home/dreadtank27/.local/lib/python3.5/site-packages/pyrogram/client/client.py", line 439, in resolve_peer
    if isinstance(chat_id, str)
KeyError: '2977xxxx'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/dreadtank27/.local/lib/python3.5/site-packages/pyrogram/session/session.py", line 237, in worker
    self.unpack_dispatch_and_ack(packet)
  File "/home/dreadtank27/.local/lib/python3.5/site-packages/pyrogram/session/session.py", line 284, in unpack_dispatch_and_ack
    self.update_handler(i.body)
  File "test.py", line 13, in callback
    client.forward_messages("2977xxxx", upd.message.to_id.channel_id, ids)
  File "/home/dreadtank27/.local/lib/python3.5/site-packages/pyrogram/client/client.py", line 537, in forward_messages
    to_peer=self.resolve_peer(chat_id),
  File "/home/dreadtank27/.local/lib/python3.5/site-packages/pyrogram/client/client.py", line 443, in resolve_peer
    raise PeerIdInvalid
pyrogram.api.errors.exceptions.bad_request_400.PeerIdInvalid: [400 PEER_ID_INVALID]: The id/access_hash combination is invalid
INFO:pyrogram.session.session:Send 8 acks

@delivrance
Copy link
Member

This means you don't have 2977xxxx in your dialogs list (i.e., you have not a conversation with him, yet), do you?

@IvanMazzoli
Copy link
Author

IvanMazzoli commented Jan 15, 2018

I thought too this was the error, but this is what I did:
• With my "normal" account, got the chat_id from one of my bots (I'm sure it's right, I use the same chat_id value for operations in MySQL without problems);
• Still with the "normal" account, talked to my MTProto account;
• Switched to my MTProto account, talked to my "normal" account;
• Trying to forward messages to my "normal" account.
If I forward to it with the @username it works fine, the problem is if I try to use the 2977xxxx identifier

@IvanMazzoli
Copy link
Author

We figured it out, now I was passing the ID as a String instead of an Int

@delivrance delivrance added the bug label Jan 24, 2018
@pyrogram pyrogram deleted a comment from slvsA Dec 16, 2018
@pyrogram pyrogram deleted a comment from slvsA Dec 16, 2018
@pyrogram pyrogram deleted a comment from slvsA Dec 16, 2018
@delivrance
Copy link
Member

@slvsA This is not the place to test whatever you are testing with the browser developer tools.

omg-xtao pushed a commit to TeamPGM/pyrogram that referenced this issue Jan 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

2 participants