'How to send a message through Pyrogram?
from pyrogram import Client
from pyrogram.errors import PeerFlood
import time
vubor = input('Нажмите Enter чтобы запустить рассылку! ')
api_id = 12905662
api_hash = "a7cfcd44cb95d26d7529d547c9a1d9ef"
app = Client('my_accont11', api_id, api_hash)
if vubor == '':
def rassulka(app):
try:
with open('username.txt', 'r') as file:
for users in file.readlines():
y = users.strip()
time.sleep(5)
app.send_message(y, 'q')
file.replace(y, '')
except PeerFlood:
print('Аккаунт заблокировн')
with app:
rassulka(app)
pyrogram.errors.exceptions.bad_request_400.PeerIdInvalid: Telegram says: [400 PEER_ID_INVALID] - The peer id being used is invalid or not known yet. Make sure you meet the peer before interacting with it
I do not know what to do
Solution 1:[1]
This error could mean several things (possible solution at the end):
- The chat id you tried to use is simply wrong, check it again.
- The chat id refers to a group or channel you are not a member of.
- The chat id argument you passed is in form of a string; you have to convert it into an integer with int(chat_id).
- The chat id refers to a user or chat your current session hasn’t met yet.
About the last point: in order for you to meet a user and thus communicate with them, you should ask yourself how to contact people using official apps. The answer is the same for Pyrogram too and involves normal usages such as searching for usernames, meeting them in a common group, having their phone contacts saved, getting a message mentioning them or obtaining the dialogs list.
possible solution
it might happen because you have passed only numerical IDs, as a string. Pyrogram tries to resolve those as a username, if it can't find those, it tries them as a phone number. If that still doesn't match, you get an error.Try to use the method with an integer, as the IDs you tried, are integers.
app.get_chat(-12905662)
{
"_": "pyrogram.Chat",
"id": -100123456789,
}
Solution 2:[2]
For write this code I used Pyrogram==1.3.6
from pyrogram import Client
from pyrogram.types import Message
from pyrogram.errors import PeerFlood
import time
vubor = input('??????? Enter ????? ????????? ????????! ')
app = Client(
"my_accont11",
api_id = 12219599,
api_hash = "a7cfcd44cb95d26d7529d547c9a1d9ef")
@app.on_message()
async def main(Client,Message):
if vubor == '':
try:
with open('username.txt', 'r') as file:
for users in file.readlines():
y = users.strip()
time.sleep(5)
await app.send_message(chat_id=Message.from_user.id,y)
new_file = file.read()
new_file.replace(y, '')
break
except PeerFlood:
print('??????? ???????????')
app.run()
For install Pyrogram 1.3.6:
pip install Pyrogram==1.3.6
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Guy Nachshon |
| Solution 2 |
