-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathProgram.cs
144 lines (130 loc) · 5.2 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Text;
using MailKit;
using MailKit.Net.Imap;
using MailKit.Search;
using Telegram.Bot;
using Telegram.Bot.Types;
using Telegram.Bot.Types.InputFiles;
namespace FabrosYandexTaxi
{
public static class Program
{
public static void Main()
{
Process.Start("/bin/bash", "-c \"sudo apt-get install xvfb libfontconfig wkhtmltopdf\"");
Process.Start("/bin/bash", "-c \"which wkhtmltopdf\"");
Process.Start("/bin/bash", "-c \"where wkhtmltopdf\"");
try
{
Run();
}
catch (Exception e)
{
SendTelegramTextMessage("Ошибка при обработке писем.", e.Message);
throw;
}
}
private static void Run()
{
var tempDirectory = Path.GetTempPath();
var baseDirectory = Path.Combine(tempDirectory, Guid.NewGuid().ToString());
Directory.CreateDirectory(baseDirectory);
var endDate = DateTime.UtcNow;
var fromDate = endDate.AddMonths(-1);
var messages = LoadMessages(fromDate);
var total = messages.Sum(m => m.Cost);
var zipStream = new MemoryStream();
using (var zipArchive = new ZipArchive(zipStream, ZipArchiveMode.Create, true))
{
foreach (var message in messages)
{
var imageStream = new MemoryStream();
message.GenerateImage(imageStream);
imageStream.Position = 0;
var pngName = message.Date.ToString("dd-MMM-yyyy HH-mm-ss") + ".png";
var entry = zipArchive.CreateEntry(pngName);
using var entryStream = entry.Open();
imageStream.CopyTo(entryStream);
}
}
zipStream.Position = 0;
SendTelegramDocumentMessage(
zipStream,
"Screenshots.zip",
$"Отчёт о поездках",
$"за период {fromDate:dd-MMM-yyyy} - {endDate:dd-MMM-yyyy}",
$"Сумма {total} BYN"
);
}
private static List<TaxiMessage> LoadMessages(DateTime fromDate)
{
var login = Environment.GetEnvironmentVariable("GMAIL_LOGIN");
var password = Environment.GetEnvironmentVariable("GMAIL_PASSWORD");
var imapClient = new ImapClient();
imapClient.Connect("imap.gmail.com", 993);
imapClient.Authenticate(login, password);
var mailbox = imapClient.Inbox;
mailbox.Open(FolderAccess.ReadOnly);
var searchQuery = SearchQuery.SentSince(fromDate)
.And(SearchQuery.FromContains("[email protected]"))
.And(SearchQuery.Or(
SearchQuery.BodyContains(TaxiMessage.ValidStreetNames[0]),
SearchQuery.BodyContains(TaxiMessage.ValidStreetNames[1]))
);
var messages = mailbox.Search(searchQuery)
.Select(id => mailbox.GetMessage(id))
.ToList();
mailbox.Close();
return messages.Select(m => new TaxiMessage(m))
.Where(m => m.HasOfficeTaxi)
.ToList();
}
private static void SendTelegramDocumentMessage(Stream stream, string name, params string[] textLines)
{
SendTelegramDocumentMessage(stream, name, JoinTextMessage(textLines));
}
private static void SendTelegramDocumentMessage(Stream stream, string name, string text)
{
var botClient = CreateTelegramBotClient();
var chatId = CreateTelegramChatId();
var file = new InputOnlineFile(stream, name);
botClient.SendDocumentAsync(chatId, file, text).Wait();
}
private static void SendTelegramTextMessage(params string[] textLines)
{
SendTelegramTextMessage(JoinTextMessage(textLines));
}
private static void SendTelegramTextMessage(string text)
{
var botClient = CreateTelegramBotClient();
var chatId = CreateTelegramChatId();
botClient.SendTextMessageAsync(chatId, text).Wait();
}
private static string JoinTextMessage(string[] textLines)
{
var sb = new StringBuilder();
foreach (var textLine in textLines.Take(textLines.Length - 1))
{
sb.AppendLine(textLine);
}
sb.Append(textLines.Last());
return sb.ToString();
}
private static TelegramBotClient CreateTelegramBotClient()
{
var botToken = Environment.GetEnvironmentVariable("TELEGRAM_BOT_TOKEN");
return new TelegramBotClient(botToken);
}
private static ChatId CreateTelegramChatId()
{
var userId = Environment.GetEnvironmentVariable("TELEGRAM_USER_ID");
return new ChatId(int.Parse(userId));
}
}
}