2

There is something weird with the passing parameter. The function is being called, I can debug it, but the request is always empty.

[EnableCors("SiteCorsPolicy")]
[ApiController]
[Route("api/[controller]")]
public class LineBotController : ControllerBase
{
    private LineMessagingClient _lineMessagingClient;

    public LineBotController()
    {
        _lineMessagingClient = new LineMessagingClient(Config._Configuration["Line:ChannelAccessToken"]);
    }

    [HttpPost]
    public async Task<HttpResponseMessage> Post(HttpRequestMessage request)
    {
        try
        {
            var events = await request.GetWebhookEventsAsync(Config._Configuration["Line:ChannelSecret"]);
            var app = new LineBotApp(_lineMessagingClient);
            await app.RunAsync(events);
        }
        catch (Exception e)
        {
            Helpers.Log.Create("ERROR! " + e.Message);
        }
        return request.CreateResponse(HttpStatusCode.OK);
    }
}

Is the HttpRequestMessage suppose to get every data from the request?

Some example of calling it:

    var data = {
        'to': 'xxx',
        'messages':[
            {
                "type": "text",
                "text": "Hello, world1"
            },
            {
                "type": "text",
                "text": "Hello, world2"
            }
        ]
    };

    $.ajax({
        type: 'POST',
        contentType: "application/json; charset=utf-8",
        dataType: 'json',
        async: false,
        data: JSON.stringify({request: data}),
        url: url,
        authorization: 'Bearer {Key}',
        success: function (success) {
            var x = success;
        },
        error: function (error) {
            var x = error;
        }
    });

the url: https://localhost/api/LineBot

9
  • show us how url is set Commented Oct 19, 2018 at 2:37
  • verify. is this asp.net-web-api-2.* or asp.net-core? Commented Oct 19, 2018 at 2:41
  • I am not sure if the url has problem because the function is being called and can be debugged. Commented Oct 19, 2018 at 2:41
  • this is asp.net core Commented Oct 19, 2018 at 2:42
  • Asp.net core no longer uses HttpRequestMessage or HttpResponseMessage so you need to clarify exactly what it is you are trying to do Commented Oct 19, 2018 at 2:44

1 Answer 1

5

Asp.net core no longer uses HttpRequestMessage or HttpResponseMessage. you would need to convert the code from the Github Repository for the WebhookRequestMessageHelper

https://github.com/pierre3/LineMessagingApi/blob/master/Line.Messaging/Webhooks/WebhookRequestMessageHelper.cs

/// <summary>
/// Verify if the request is valid, then returns LINE Webhook events from the request
/// </summary>
/// <param name="request">HttpRequestMessage</param>
/// <param name="channelSecret">ChannelSecret</param>
/// <returns>List of WebhookEvent</returns>
public static async Task<IEnumerable<WebhookEvent>> GetWebhookEventsAsync(this HttpRequestMessage request, string channelSecret)
{
    if (request == null) { throw new ArgumentNullException(nameof(request)); }
    if (channelSecret == null) { throw new ArgumentNullException(nameof(channelSecret)); }

    var content = await request.Content.ReadAsStringAsync();

    var xLineSignature = request.Headers.GetValues("X-Line-Signature").FirstOrDefault();
    if (string.IsNullOrEmpty(xLineSignature) || !VerifySignature(channelSecret, xLineSignature, content))
    {
        throw new InvalidSignatureException("Signature validation faild.");
    }
    return WebhookEventParser.Parse(content);
}

so that it will work in .net core.

[HttpPost]
public async Task<IActionResult> Post([FromBody] string content) {
    try {              
        var events = WebhookEventParser.Parse(content);
        var app = new LineBotApp(_lineMessagingClient);
        await app.RunAsync(events);
    } catch (Exception e) {
        Helpers.Log.Create("ERROR! " + e.Message);
    }
    return Ok();
}

This is meant to be a simplified example which does not verify signature.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.