gabrieldwight / Whatsapp-Business-Cloud-Api-Net

This is C# wrapper of whatsapp business cloud api for .NET
MIT License
284 stars 104 forks source link

Error when sending messages #7

Closed spadillez closed 1 year ago

spadillez commented 1 year ago

{"An invalid request URI was provided. The request URI must either be an absolute URI or BaseAddress must be set."}

Startup.CS WhatsAppBusinessCloudApiConfig whatsAppConfig = new WhatsAppBusinessCloudApiConfig(); whatsAppConfig.WhatsAppBusinessPhoneNumberId = Configuration.GetSection("WhatsAppBusinessCloudApiConfiguration")["WhatsAppBusinessPhoneNumberId"]; whatsAppConfig.WhatsAppBusinessAccountId = Configuration.GetSection("WhatsAppBusinessCloudApiConfiguration")["WhatsAppBusinessAccountId"]; whatsAppConfig.WhatsAppBusinessId = Configuration.GetSection("WhatsAppBusinessCloudApiConfiguration")["WhatsAppBusinessId"]; whatsAppConfig.AccessToken = Configuration.GetSection("WhatsAppBusinessCloudApiConfiguration")["AccessToken"]; services.AddWhatsAppBusinessCloudApiService(whatsAppConfig);

Controller

try
    {
        TextMessageRequest textMessageRequest = new TextMessageRequest();
        textMessageRequest.To = textMessageLast.From;
        textMessageRequest.Text = new WhatsAppText();
        textMessageRequest.Text.Body = "Test";
        textMessageRequest.Text.PreviewUrl = false;

        var results = await _whatsAppBusinessClient.SendTextMessageAsync(textMessageRequest);

        return Ok();
    }
    catch (WhatsappBusinessCloudAPIException ex)
    {
        return Ok(ex.Message);
    }
spadillez commented 1 year ago

Forget to mencion using .netcore

gabrieldwight commented 1 year ago

@spadillez Hi, on my side I have no issue with the invalid request URI. Have you initialized the WhatsApp client in your controller class with dependency injection?

spadillez commented 1 year ago

Yes

      public WhatsappController(
            TipoSolicitacaoService tipoSolicitacaoService,
            UrgenciaSolicitacaoService urgenciaSolicitacaoService,
            SolicitacaoUsuarioService solicitacaoUsuarioService,
            UsuarioServico usuarioService,
            IWhatsAppBusinessClient whatsAppBusinessClient)
        {
            _tipoSolicitacaoService = tipoSolicitacaoService;
            _urgenciaSolicitacaoService = urgenciaSolicitacaoService;
            _solicitacaoUsuarioService = solicitacaoUsuarioService;
            _usuarioService = usuarioService;
            _whatsAppBusinessClient = whatsAppBusinessClient;
        }
gabrieldwight commented 1 year ago

You can send me a sample repo without the credentials so I can have a look what is happening with the request URI on your end.

spadillez commented 1 year ago

Yes, i have find a way to get it work, using the legacy injection

`WhatsAppBusinessCloudApiConfig whatsAppConfig = new WhatsAppBusinessCloudApiConfig(); whatsAppConfig.WhatsAppBusinessPhoneNumberId = configuration.GetSection("WhatsAppBusinessCloudApiConfiguration")["WhatsAppBusinessPhoneNumberId"]; whatsAppConfig.WhatsAppBusinessAccountId = configuration.GetSection("WhatsAppBusinessCloudApiConfiguration")["WhatsAppBusinessAccountId"]; whatsAppConfig.WhatsAppBusinessId = configuration.GetSection("WhatsAppBusinessCloudApiConfiguration")["WhatsAppBusinessId"]; whatsAppConfig.AccessToken = configuration.GetSection("WhatsAppBusinessCloudApiConfiguration")["AccessToken"];

        var httpClient = new HttpClient();

        httpClient.BaseAddress = WhatsAppBusinessRequestEndpoint.BaseAddress;

        //create Mpesa API client instance
        _whatsAppBusinessClient = new WhatsAppBusinessClient(httpClient, whatsAppConfig);`

Tried to find why the httpclient got without the base url when using the DInjection but without success i will make a sample for you. But atleast it worked for now

gabrieldwight commented 1 year ago

Alright. I will investigate further the NET 6 builder for httpclient why it is not setting up the base url.