瀏覽代碼

//

master
Dunja Stevanovic 3 年之前
父節點
當前提交
b183690621

+ 1
- 0
BlackRockReportFunction/BlackRockReportFunction.csproj 查看文件

@@ -21,6 +21,7 @@
<PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.6.0" />
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.SendGrid" Version="3.0.2" />
<PackageReference Include="Microsoft.Build.Tasks.Core" Version="17.2.0" />
<PackageReference Include="RestSharp" Version="108.0.1" />
<PackageReference Include="System.Core" Version="3.5.21022.801" />
<PackageReference Include="System.Drawing.Common" Version="6.0.0" />
<PackageReference Include="System.IO.Packaging" Version="6.0.0" />

+ 5
- 25
BlackRockReportFunction/ClockifyApiIntegrationFunction.cs 查看文件

@@ -17,14 +17,13 @@ namespace BlackRockReportFunction
{
private readonly ILogger _logger;
public static string? clockifyApiKey = Environment.GetEnvironmentVariable("ClockifyApiKey");
public static string? workspaceId = Environment.GetEnvironmentVariable("ClockifyWorkspaceId");

static HttpClient client = new HttpClient();

public static async Task InitializeClockifyIntegration()
public static void InitializeClockifyIntegration()
{
//client.DefaultRequestHeaders.Add("X-API-Key", clockifyApiKey);

client.DefaultRequestHeaders.Add("X-API-Key", "MmU2ZTA2MGItMTM1ZS00ZTg1LTkwMjAtMDkzYThiZmNmYmIy");
client.DefaultRequestHeaders.Add("X-API-Key", clockifyApiKey);

client.BaseAddress = new Uri("https://reports.api.clockify.me/v1");

@@ -40,7 +39,7 @@ namespace BlackRockReportFunction

[Function("ClockifyApiIntegrationFunction")]
[QueueOutput("queue1")]
public string Run([TimerTrigger("*/15 * * * * *")] TimerInfo myTimer) //TODO: Set on Friday at 20 o'clock "0 0 20 * * 5"
public string Run([TimerTrigger("*/15 * * * * *")] TimerInfo myTimer) //TODO: */15 * * * * *
{
InitializeClockifyIntegration(); // Clockify API Integration

@@ -51,7 +50,7 @@ namespace BlackRockReportFunction

HttpContent httpContent = new StringContent(json, Encoding.UTF8, "application/json");

HttpResponseMessage response = client.PostAsync(client.BaseAddress + "/workspaces/5eb44340ef0f6c66fc88732a/reports/summary", httpContent).Result;
HttpResponseMessage response = client.PostAsync(client.BaseAddress + "/workspaces/"+workspaceId+"/reports/summary", httpContent).Result;
@@ -73,25 +72,6 @@ namespace BlackRockReportFunction
}
}

//public class MyInfo
//{
// public MyScheduleStatus ScheduleStatus { get; set; }

// public bool IsPastDue { get; set; }
//}

//public class MyScheduleStatus
//{
// public DateTime Last { get; set; }

// public DateTime Next { get; set; }

// public DateTime LastUpdated { get; set; }



//}


}

+ 57
- 34
BlackRockReportFunction/MailSenderFunction.cs 查看文件

@@ -2,7 +2,7 @@ using System;
using System.IO;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.Logging;
//using SendGrid.Helpers.Mail;
using Microsoft.Azure.WebJobs;
using MailKit;
using MimeKit;
@@ -10,6 +10,9 @@ using MimeKit.Text;
using MailKit.Net.Smtp;
using MailKit.Security;
using GemBox.Spreadsheet;
using RestSharp;
using RestSharp.Authenticators;
//using System.Net.Mail;

namespace BlackRockReportFunction
{
@@ -17,6 +20,11 @@ namespace BlackRockReportFunction
{
private readonly ILogger _logger;

private static string SenderAddress = Environment.GetEnvironmentVariable("SenderAdress");
private static string SenderDisplayName = Environment.GetEnvironmentVariable("SenderDisplayName");
private static string MailGunApiKey = Environment.GetEnvironmentVariable("MailGunApiKey");
private static string Domain = Environment.GetEnvironmentVariable("Domain");
private static string MailTo = Environment.GetEnvironmentVariable("MailTo");
public MailSenderFunction(ILoggerFactory loggerFactory)
{
_logger = loggerFactory.CreateLogger<MailSenderFunction>();
@@ -25,48 +33,63 @@ namespace BlackRockReportFunction
[Function("MailSenderFunction")]
public void Run([BlobTrigger("report-container/{name}", Connection = "AzureWebJobsStorage")] byte[] fileData)
{
//var msg = new SendGridMessage()
string fileName = string.Format($"BlackRockReport_{DateTime.Now.ToString("f")}.xlsx");

var client = new RestClient("https://api.mailgun.net/v3");
client.Authenticator = new HttpBasicAuthenticator("api", MailGunApiKey);


MemoryStream memoryStream = new MemoryStream(fileData);
memoryStream.Position = 0;

RestRequest request = new RestRequest();
request.AddParameter("domain", Domain, ParameterType.UrlSegment);
request.Resource = "{domain}/messages";
request.AddParameter("from", $"{SenderDisplayName} <{SenderAddress}>");
request.AddParameter("to", MailTo);
request.AddParameter("subject", "BlackRock Report");
request.AddParameter("html", string.Format("Here is yours report for current week. {0}", fileName));
request.AddFile("attachment", memoryStream.ToArray(), fileName);
request.Method = Method.Post;
client.Execute(request);

//var email = new MimeMessage();
//email.From.Add(MailboxAddress.Parse("[email protected]"));
//email.To.Add(MailboxAddress.Parse("[email protected]"));
//email.Sender = MailboxAddress.Parse("Clockify");

//email.Subject = "BlackRock Report";
//var body = new TextPart(TextFormat.Html) { Text = string.Format("Here is yours report for last week. {0}", fileName) };

//MemoryStream memoryStream = new MemoryStream(fileData);
//memoryStream.Position = 0;



//var attachment = new MimePart(" application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
//{
// From = new EmailAddress("[email protected]", "Nikola Jovanovic"),
// Subject = "Test SendGrid Azure Function",
// PlainTextContent = String.Format("If you read this text, then congratulations," +
// " you did it! :)")
// Content = new MimeContent(memoryStream),
// ContentDisposition = new ContentDisposition(ContentDisposition.Attachment),
// ContentTransferEncoding = ContentEncoding.Base64,
// FileName = fileName
//};

//msg.AddTo(new EmailAddress("[email protected]", "Nikola Jovanovic"));
//var multipart = new Multipart("mixed");
//multipart.Add(body);
//multipart.Add(attachment);
//email.Body = multipart;

//return msg;
string fileName = string.Format($"BlackRockReport_{DateTime.Now.ToString("f")}.xlsx");

var email = new MimeMessage();
email.From.Add(MailboxAddress.Parse("[email protected]"));
email.To.Add(MailboxAddress.Parse("[email protected]"));
email.Subject = "BlackRock Report";
var body = new TextPart(TextFormat.Html) { Text = string.Format("Here is yours report for last week. {0}", fileName) };
//SmtpClient smtp = new SmtpClient();
//smtp.Connect("smtp.gmail.com", 587, SecureSocketOptions.StartTls);

MemoryStream memoryStream = new MemoryStream(fileData);
memoryStream.Position = 0;
//smtp.Authenticate("[email protected]", "Nacional99!"); //"[email protected]", "ecsdGZxWHk4yfjZpD5"
//smtp.Send(email);
//smtp.Disconnect(true);

var attachment = new MimePart(" application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
{
Content = new MimeContent(memoryStream),
ContentDisposition = new ContentDisposition(ContentDisposition.Attachment),
ContentTransferEncoding = ContentEncoding.Base64,
FileName = fileName
};

var multipart = new Multipart("mixed");
multipart.Add(body);
multipart.Add(attachment);
email.Body = multipart;

SmtpClient smtp = new SmtpClient();
smtp.Connect("smtp.ethereal.email", 587, SecureSocketOptions.StartTls);
smtp.Authenticate("[email protected]", "ecsdGZxWHk4yfjZpD5"); //"[email protected]", "8pPsjCbwCFMrEeKNef"
smtp.Send(email);
smtp.Disconnect(true);

_logger.LogInformation("Email sent successfully!");
}

Loading…
取消
儲存