Closed sjosegarcia closed 3 years ago
Hi @sjosegarcia ,
This error occurs because you use Provider
wiring marker. It injects provider, but not an instance provided by provider. To fix the problem use Provide
marker:
bot_service: BotService = Depends(Provide[BotContainer.bot_provider]),
Closing the issue. Feel free to comment or open a new issue if you need any other help.
Sorry about the late response, but now I ran across another issue. I was wondering what might be causing this?
AttributeError: 'Provide' object has no attribute 'set_webhook'
bot_container.py
from providers.bot_repository import BotRepository
from providers.token_service import TokenService
from providers.ban_service import BanService
from providers.bot_service import BotService
from providers.cas_service import CasService
from providers.chat_id_service import ChatIdService
from providers.link_removal_service import LinkRemovalService
from providers.welcome_message_service import WelcomeMessageService
from dependency_injector.containers import DeclarativeContainer
from dependency_injector.providers import ThreadSafeSingleton, Singleton
class BotContainer(DeclarativeContainer):
bot_repository = ThreadSafeSingleton(BotRepository)
bot_service = Singleton(BotService, bot_repository=bot_repository)
token_service = Singleton(TokenService, bot_repository=bot_repository)
ban_service = Singleton(BanService, bot_repository=bot_repository)
cas_service = Singleton(CasService, bot_repository=bot_repository)
chatid_service = Singleton(ChatIdService, bot_repository=bot_repository)
link_removal_service = Singleton(LinkRemovalService, bot_repository=bot_repository)
welcome_message_service = Singleton(
WelcomeMessageService, bot_repository=bot_repository
)
bot_endpoints.py
from fastapi import APIRouter
from telegram import Update
from fastapi.requests import Request
from setup.config import get_settings
from fastapi.responses import ORJSONResponse
from dependency_injector.wiring import inject, Provide
from fastapi import Depends
from containers.bot_container import BotContainer, BotService, BotRepository
telegram_bot_router = APIRouter()
@telegram_bot_router.get(f"/{get_settings().telegram_bot_api_key}/setwebhook")
@inject
async def bot_webhook(
bot_service: BotService = Depends(Provide[BotContainer.bot_service]),
) -> ORJSONResponse:
return ORJSONResponse({"hooked": bot_service.set_webhook()})
@telegram_bot_router.get(f"/{get_settings().telegram_bot_api_key}/webhookinfo")
@inject
async def bot_webhook_info(
bot_repository: BotRepository = Depends(Provide[BotContainer.bot_repository]),
) -> ORJSONResponse:
return ORJSONResponse(bot_repository.updater.bot.get_webhook_info().to_dict())
@telegram_bot_router.post(f"/{get_settings().telegram_bot_api_key}")
@inject
async def bot_index(
request: Request,
bot_repository: BotRepository = Depends(Provide[BotContainer.bot_repository]),
) -> None:
data = await request.json()
bot_repository.updater.dispatcher.process_update(
Update.de_json(data, bot_repository.updater.dispatcher.bot)
)
I fixed the issue above, and I am in a full circle. It now displays the same error with what I opened with.
AttributeError: 'dependency_injector.providers.ThreadSafeSingleton' object has no attribute 'updater'
at bot_index (/app/bot/endpoints/bot_endpoints.py:37)
BotRepository
from setup.config import get_settings
from telegram.ext import Updater
from telegram import Bot
class BotRepository:
def __init__(self) -> None:
bot = Bot(get_settings().telegram_bot_api_key)
self.updater = Updater(bot=bot, workers=1)
I got it working. I am closing the issue. I needed to add the config into the container.
For some reason I am getting an attribute error. Here is the stack: dependency-injector = ">=4.32.0"
bot_service.py
bot_container.py
start_web.py
bot_endpoints.py