Reputation: 171
This is a follow up from a question which was asked here: how to get messages of the public channels from Telegram
The code here used was:
import asyncio
from telethon import TelegramClient
from telethon.tl import functions, types
client = TelegramClient('YOUR_SESSION_NAME', 'YOUR_API_ID', 'YOUR_API_HASH')
client.start()
async def main():
channel = await client.get_entity('CHANNEL USERNAME')
messages = await client.get_messages(channel, limit= None) #pass your own args
#then if you want to get all the messages text
for x in messages:
print(x.text) #return message.text
In this code YOUR_SESSION_NAME
was used and i am not sure what that is. I have gone through the documentation here: https://docs.telethon.dev/en/latest/concepts/sessions.html But since i am very new to python I am getting stuck here. I do have 'YOUR_API_ID'
and 'YOUR_API_HASH'
.
As of now when I run this code I am getting the following error: Full Error below
---------------------------------------------------------------------------
OperationalError Traceback (most recent call last)
<ipython-input-2-de73a24697a1> in <module>
3 from telethon.tl import functions, types
4
----> 5 client = TelegramClient('anon', '[code]', '[code]')
6 client.start()
7
C:\ProgramData\Anaconda3\lib\site-packages\telethon\client\telegrambaseclient.py in __init__(self, session, api_id, api_hash, connection, use_ipv6, proxy, local_addr, timeout, request_retries, connection_retries, retry_delay, auto_reconnect, sequential_updates, flood_sleep_threshold, raise_last_call_error, device_model, system_version, app_version, lang_code, system_lang_code, loop, base_logger)
279 if (not session.server_address or
280 (':' in session.server_address) != use_ipv6):
--> 281 session.set_dc(
282 DEFAULT_DC_ID,
283 DEFAULT_IPV6_IP if self._use_ipv6 else DEFAULT_IPV4_IP,
C:\ProgramData\Anaconda3\lib\site-packages\telethon\sessions\sqlite.py in set_dc(self, dc_id, server_address, port)
166 def set_dc(self, dc_id, server_address, port):
167 super().set_dc(dc_id, server_address, port)
--> 168 self._update_session_table()
169
170 # Fetch the auth_key corresponding to this data center
C:\ProgramData\Anaconda3\lib\site-packages\telethon\sessions\sqlite.py in _update_session_table(self)
192 # some more work before being able to save auth_key's for
193 # multiple DCs. Probably done differently.
--> 194 c.execute('delete from sessions')
195 c.execute('insert or replace into sessions values (?,?,?,?,?)', (
196 self._dc_id,
OperationalError: database is locked
Could you please give me some pointers as to what I am doing wrong? My objective is to print the text log of all the chat as it is coming in so i can run a search filer for a keyword to run an alert.
Upvotes: 1
Views: 2118
Reputation: 36
You can use any session name you come up with. For example, you might write:
...
session_name = "Test Session"
client = TelegramClient(session_name, 'YOUR_API_ID', 'YOUR_API_HASH')
client.start()
...
Then Telegram will ask you to confirm that this is you via sending code to your TG client (web/mobile). If you will use this session name again - that confirming won't show up. Else you will re-name this one, you'll need to confirm again.
By the way, you can use input() function to create your session name, stuff like that:
...
session_name = input("Enter your session name: ")
client = TelegramClient(session_name, 'YOUR_API_ID', 'YOUR_API_HASH')
client.start()
...
Then you'll have to make an confirmation. Notice that you if you'll use input() - don't forget to type the same name evry time; if you will take mistake at least with one symbol - you have to confirm again.
Upvotes: 2