55 lines
2.2 KiB
C#
55 lines
2.2 KiB
C#
|
using BackendWorkerService.Services.Interface;
|
|||
|
using Microsoft. Extensions.Logging;
|
|||
|
using Newtonsoft.Json.Linq;
|
|||
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.IO;
|
|||
|
using System.Net;
|
|||
|
using System.Text;
|
|||
|
|
|||
|
namespace BackendWorkerService.Services.Implement
|
|||
|
{
|
|||
|
class SendLineNotifyService: ISendLineNotifyService
|
|||
|
{
|
|||
|
|
|||
|
private readonly ILogger<SendLineNotifyService> logger;
|
|||
|
|
|||
|
public SendLineNotifyService(ILogger<SendLineNotifyService> logger)
|
|||
|
{
|
|||
|
this.logger = logger;
|
|||
|
}
|
|||
|
|
|||
|
public bool Send(int id, string lineToken, string message)
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
logger.LogInformation("【SendLineNotifyService】【Line Notify開始發送】[任務編號]:{0}", id);
|
|||
|
HttpWebRequest Postrequest = (HttpWebRequest)WebRequest.Create("https://notify-api.line.me/api/notify?message=" + message);
|
|||
|
Postrequest.Method = "POST";
|
|||
|
Postrequest.Headers.Add("Authorization", "Bearer " + lineToken);
|
|||
|
Postrequest.PreAuthenticate = true;
|
|||
|
HttpWebResponse response = (HttpWebResponse)Postrequest.GetResponse();
|
|||
|
var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
|
|||
|
var final = JObject.Parse(responseString);
|
|||
|
var get = final["status"].ToString();
|
|||
|
if (get != "200")
|
|||
|
{
|
|||
|
logger.LogError("【SendLineNotifyService】【Line Notify發送失敗】[任務編號]:{0}", id);
|
|||
|
logger.LogError("【SendLineNotifyService】【Line Notify發送失敗】[失敗內容]:{0}", responseString);
|
|||
|
return false;
|
|||
|
}
|
|||
|
|
|||
|
logger.LogInformation("【SendLineNotifyService】【Line Notify發送成功】[任務編號]:{0}", id);
|
|||
|
return true;
|
|||
|
}
|
|||
|
catch (Exception exception)
|
|||
|
{
|
|||
|
logger.LogError("【SendLineNotifyService】【Line Notify發送失敗】[任務編號]:{0}", id);
|
|||
|
logger.LogError("【SendLineNotifyService】【Line Notify發送失敗】[Exception]:{0}", exception.ToString());
|
|||
|
throw;
|
|||
|
//return false;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|