您需要使用 LINE 的 SDK 來實現
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Line.Messaging;
using Line.Messaging.Webhooks;
using HtmlAgilityPack;
namespace YourNameSpace
{
[Route("api/[controller]")]
[ApiController]
public class LineBotWebhookController : ControllerBase
{
private readonly IConfiguration _config;
public LineBotWebhookController(IConfiguration config)
{
_config = config;
}
[HttpPost]
public async Task<IActionResult> Post([FromBody] LineBotWebhookRequest request)
{
var channelAccessToken = _config.GetValue<string>("LineBot:ChannelAccessToken");
var lineClient = new LineMessagingClient(channelAccessToken);
var events = request.Events;
var replyToken = events[0].ReplyToken;
var message = events[0].Message.Text;
var response = new LineBotResponse();
response.ReplyToken = replyToken;
if (message.Contains("爬蟲"))
{
var web = new HtmlWeb();
var doc = web.Load("https://zh.wikipedia.org/wiki/%E7%88%AC%E8%9F%B2");
var title = doc.DocumentNode.SelectSingleNode("//h1[@id='firstHeading']").InnerText;
response.Messages.Add(new TextMessage(title));
}
else
{
response.Messages.Add(new TextMessage("Hello, World!"));
}
await lineClient.ReplyMessageAsync(response);
return Ok();
}
}
}
範例程式僅供參考,你需要根據自身情況斟酌修改。