I got a little problem that make stuck me at code small script that collect data from telegram chats. For first i will show my code and config file:
Config.ini:
[account]
api_id = xxxx
api_hash = xxxx
[parser]
channels_to_parse = [-xxxx,-xxxx]
Run.py
import configparser
import asyncio
import time
from telethon import events
from telethon import TelegramClient
from telethon.tl import functions, types
from datetime import datetime
#load config file
config = configparser.ConfigParser()
config.read('config.ini', encoding="utf-8")
#telethon init
client = TelegramClient('sess', config.get("account", 'api_id'), config.get("account", 'api_hash'))
client.start()
#main cycle
@client.on(events.NewMessage(chats=config.get('parser' , 'channels_to_parse')))
async def main(event):
#some code...
client.run_until_disconnected()
The main problem goes from string that contains arguments for telethon that points chats IDs from which i collecting data:
ValueError: Cannot find any entity corresponding to "[-xxxx]"
When i passing arguments manually, without configparser:
@client.on(events.NewMessage(chats = [-xxxx, -xxxx]))
Everything works well. So i think that issue related to configparser or configparser parameters. I checked configparser docs and didn't find anything that can help me.
I already tried to use channels name instead IDs. Maybe who's can explain me what i do wrong.
I can't check this at the moment, but I have an idea that configparser returns the str type and you need a list. But I could be wrong(
UPDATE
I checked it out and I was right!
config.ini:
[parser]
channels_to_parse = -xxxx -xxxx
file.py:
parser = config.get('parser', 'channels_to_parse') # type str
chats = [int(i) for i in parser.split()] # type list
@client.on(events.NewMessage(chats=chats))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.