Exemple #1
0
async def subscribe(event: NewMessage.Event):
    """
    Subscribe user to remote resource.
    
    This handler expects 2 arguments: resource name (currently only telegram) and link to post from this resource.
    
    If resource is not supported, we will return message that we don't support this type of resource.
    
    :param event: 
    :return: 
    """
    user: User = event.message.sender
    resource_name, channel_info = event.message.text.split()[-2:]

    if resource_name == "telegram":
        channel_name, post_id = parse_post_url(channel_info)

        if channel_name is None:
            message = (
                f"Url you supplied was wrong, it should be like https://t.me/channel_name/79",
            )
        else:
            try:
                channel_id = (await client.get_input_entity(channel_name)).channel_id
                UserContainer.subscribe(user.id, channel_id, "telegram")
                TelegramResourcesContainer.add_new_resource(channel_name, channel_id, post_id)
                message = f"You have subscribed to {channel_name}"
            except Exception as e:
                message = "What you have supplied is not a channel"
                logger.warning("User {} supplied wrong entity, {}", user.id, str(e))
    else:
        message = f"{resource_name} is not supported"

    await event.respond(message)
Exemple #2
0
def test_tg_subscribe(tg_client: TelegramClient, mongo_client):
    uc = UserContainer.let(mc=mongo_client)
    trc = TelegramResourcesContainer.let(mc=mongo_client)

    channel_id = tg_client.get_input_entity("popyachsa").channel_id
    uc.subscribe(1, channel_id, "telegram")
    trc.add_new_resource("popyachsa", channel_id, 28530)
Exemple #3
0
def test_tg_fetch(tg_client: TelegramClient, mongo_client):
    trc = TelegramResourcesContainer.let(mc=mongo_client)
    fetch = FetchNewTelegramPosts(db=trc, tg_client=tg_client)
    loop = asyncio.get_event_loop()
    res = loop.run_until_complete(fetch())
    print(res)
    assert len(res) > 0
Exemple #4
0
async def list_channels(event: NewMessage.Event):
    user: User = event.message.sender
    resources: Dict[str, List[str]] = UserContainer.list_subscriptions(user.id)
    result = {
        "telegram": TelegramResourcesContainer.get_resources_names(resources.get("telegram", []))
    }
    await event.respond(pprint.pformat(result))