pyrogram / pyrogram

Elegant, modern and asynchronous Telegram MTProto API framework in Python for users and bots
https://pyrogram.org
GNU Lesser General Public License v3.0
4.23k stars 1.37k forks source link

Can't forward messages to anyone but myself #3

Closed IvanMazzoli closed 6 years ago

IvanMazzoli commented 6 years ago

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 commented 6 years ago

@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 commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

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 ;)

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 commented 6 years ago

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

IvanMazzoli commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

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

IvanMazzoli commented 6 years ago

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 commented 6 years ago

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

delivrance commented 5 years ago

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