iT邦幫忙

1

關於C# LINE BOT 爬蟲

  • 分享至 

  • xImage

這邊有人有關於 C# LINE BOT 爬蟲 相關CODE嗎?? 或者可以私訊 加我好友~ 謝謝

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

0
JamesDoge
iT邦高手 1 級 ‧ 2023-01-29 05:07:40

您需要使用 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();
        }
    }
}

範例程式僅供參考,你需要根據自身情況斟酌修改。

我要發表回答

立即登入回答