so i`m trying to learn Telethon and wrote some script from tutorial. It works fine for the first time, when i have to confirm authorization.
from telethon import TelegramClient
api_id = 1234567
api_hash = ''
client = TelegramClient('anon', api_id, api_hash)
async def main():
me = await client.get_me()
print(me.username)
with client:
client.loop.run_until_complete(main())
but when i try to run it again with the same .session file i keep getting same error, which doesn`t help to understand the problem.
Server sent a very new message with ID 7144059125492611077, ignoring
Server sent a very new message with ID 7144059125964561409, ignoring
Also, program doesn`t stop running after this error.
After a couple of days searching for a fix, i just switched to pyrogram, which works perfectly fine. So if someone got the same problem I recommend just to change Telethon to something else.
Make sure your system time is correct. If the issue persists, it may be a bug you which you should report to https://github.com/LonamiWebs/Telethon/issues/new/choose. However recent versions of the library (v1.25.2) should not have this issue.
Related
I have a python telegram bot running on my laptop. I am trying to run the same script on my Raspberry Pi4 but i am getting an warning message when I try to run it which isnt happening on the laptop. I am using python 3.9 on both systems and I have installed the same packages.
On the laptop this works fine, however when I ssh to the Pi to run it I get the warning below, when running direct from command line and through vscode ssh.
This is the warning message 'RuntimeWarning: coroutine 'Bot.send_message' was never awaited
bot.send_message(chat_id=chat_id, text=f'{message}')
RuntimeWarning: Enable tracemalloc to get the object allocation traceback'
Any help will be greatly appreciated, thank you.
# Telegram bot practice
from bs4 import BeautifulSoup as bs
from urllib import request
import telegram
import json
chat_id = '********'
token = '***********************************'
bot = telegram.Bot(token=token)
def send_message(message):
bot.send_message(chat_id=chat_id, text=f'{message}')
send_message('hey there')
I now have it working, I imported asyncio and used asyncio.run for the sending of the message.
I still have no idea why i had this issue on one system and not the oter buy either way it is now working.
from bs4 import BeautifulSoup as bs
from urllib import request
import telegram
import json
import asyncio
chat_id = '********'
token = '***********************************'
bot = telegram.Bot(token=token)
async def send_message(message):
bot.send_message(chat_id=chat_id, text=f'{message}')
asyncio.run(send_message('hey there'))
I wrote a simple script on python to copy messages in one chat to the other. The destination chat is a chat with a telegram bot.
It works perfectly on my computer, but I wanted it to work on my Raspberry Pi (not to keep my Windows computer ON). When I started the script on Raspberry it started to place the error:
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
Traceback (most recent call last):
File "/usr/local/lib/python3.7/dist-packages/pyrogram/methods/advanced/resolve_peer.py", line 60, in resolve_peer
return await self.storage.get_peer_by_id(peer_id)
File "/usr/local/lib/python3.7/dist-packages/pyrogram/storage/sqlite_storage.py", line 148, in get_peer_by_id
raise KeyError(f"ID not found: {peer_id}")
KeyError: 'ID not found: 1321800342'
So I checked all didgets, everything is right, checked the chat id again. I have an idia, that the problem is that exactly that device (Raspberry Pi) didn't write to that bot itself. But maybe I am wrong.
Did anyone has the same issue? Do you have any idias how to solve it?
Thanks a lot.
from pyrogram import Client, filters
# Target chat. Can also be a list of multiple chat ids/usernames
TARGET = -730374088
app = Client("my_account")
# Filter in only new_chat_members updates generated in TARGET chat
#app.on_message(filters.chat(TARGET) & filters.text)
async def welcome(client, message):
await app.send_message(1321800342, message.text)
app.run() # Automatically start() and idle()
The new session on your Pi likely hasn't seen the chat/channel, and thus doesn't know what to associate the ID to. Make sure the new session receives any kind of update from the chat/channel and read Pyrogram's FAQ section on the error.
Worked well when I changed chat id to the bot's username.
I have been sending Telegram messages to a chat using the code below. It worked fine but the group has been upgraded to a "Supergoup" and it has stopped working. It just gives a "Chat cannot be found" error.
I have the new ID and found a suggestion that I needed to add the bot as admin for the group. Neither of these has fixed the problem.
Does anyone know what the issue is?
bot = telegram.Bot(token='xxxxxxxxxxxxxxxxxxxxxxxxxxx')
def post(message):
msg = dedent(f'''\
On subject {subject}: {message}
''')
bot.send_message('-1111111111', text=msg)
I have create a telegram bot in python but I am unable to set webhook. After some research I have set the webhook but it's not working. the code is working with start_polling() method but stop working in heroku server after some time so I want to run the code using webhook.
The code is running properly but not getting any response from telegram
PORT = int(os.environ.get('PORT', '5000'))
updater.start_webhook(listen="0.0.0.0", port=PORT, url_path=token,
webhook_url='https://git.heroku.com/pytelestock.git' + token)
When run the api got this error.
{"ok":true,"result":{"url":"https://git.heroku.com/pytelestock.git1851418449:token","has_custom_certificate":false,"pending_update_count":2,"last_error_date":1622897956,"last_error_message":"Wrong response from the webhook: 404 Not Found","max_connections":40,"ip_address":"xx.xxx.xxx.xxx"}}
As i am new in this field i don't know why i am getting the error and how to solve this.
The webhook_url should rather be 'pytelestock.herouk.com/' + token.
Hi all I am hoping someone here can help me with the jsonrpcclient using websockets. I am very new to using websockets and have to use jsonrpc for a project. I have searched the site but unfortunately I was unable to find the answer.
Below is the sample code from the documention which I can get working. But I wish to create the websockets.connect then use it with multiple responses.
【Base Code】
import asyncio
import websockets
from jsonrpcclient.clients.websockets_client import WebSocketsClient
async def main():
async with websockets.connect("ws://localhost:5000") as ws:
response = await WebSocketsClient(ws).request("ping")
print(response.data.result)
asyncio.get_event_loop().run_until_complete(main())
【What I Want】
import asyncio
import websockets
from jsonrpcclient.clients.websockets_client import WebSocketsClient
async def main():
async with websockets.connect("ws://localhost:5000") as ws:
response = await WebSocketsClient(ws).request("ping")
print(response.data.result)
response2 = await WebSocketsClient(ws).request("donkey")
print(response2.data.result)
asyncio.get_event_loop().run_until_complete(main())
Additional Question: Is it possible to set the client up like a server so it is always running and listing and if it gets a command from another script that it will call the main function?
Thank you in advance for any help!
https://jsonrpcclient.readthedocs.io/en/latest/examples.html#websockets
OS: Windows
Python: 3.8
Except Error when running code similar to 'What I want': code = 1000 (OK) No reason
EDIT
After trial and error and lots of Googling it seems you just have to have the server running in a while loop to process multiple requests in a row like I am after.
However if someone finds out a way to do this with out putting the server in a while loop I would love to hear how.
Thank you.