This commit is contained in:
2021-02-05 17:45:08 +03:00
commit 0250d37ad2
9 changed files with 493 additions and 0 deletions
+25
View File
@@ -0,0 +1,25 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.30320.27
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RgDevBot", "RgDevBot\RgDevBot.csproj", "{2BAF765F-8E7E-4544-87D5-483EA9FECCA7}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{2BAF765F-8E7E-4544-87D5-483EA9FECCA7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2BAF765F-8E7E-4544-87D5-483EA9FECCA7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2BAF765F-8E7E-4544-87D5-483EA9FECCA7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2BAF765F-8E7E-4544-87D5-483EA9FECCA7}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {9ACBCBC1-5D08-41F1-868C-F21CBF396874}
EndGlobalSection
EndGlobal
+57
View File
@@ -0,0 +1,57 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.Json.Serialization;
using Newtonsoft.Json;
using RgDevBot.ObjectModel;
namespace RgDevBot
{
public class NewsParser
{
private readonly TelegramBot _bot;
private int _lastSentId = -1;
public NewsParser(TelegramBot bot)
{
_bot = bot;
}
public void Parse()
{
var url = "https://rg-dev.ru/api/news/?type=news";
var content = Get(url);
var news = JsonConvert.DeserializeObject<NewsListResponse>(content);
var latestNews = news.results.OrderByDescending(f => f.id).FirstOrDefault();
if (latestNews != null && latestNews.id != _lastSentId)
{
var text = $"{latestNews.title}:\r\nhttps://rg-dev.ru/press/news/all/{latestNews.id}/";
Console.WriteLine(text);
_bot.SendMessage(text);
_lastSentId = latestNews.id;
}
}
public string Get(string url, string contentType = "application/json")
{
var httpRequest = (HttpWebRequest)WebRequest.Create(url);
httpRequest.ContentType = contentType;
httpRequest.Method = "GET";
var httpResponse = (HttpWebResponse)httpRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
return result;
}
}
}
}
+18
View File
@@ -0,0 +1,18 @@
using System;
namespace RgDevBot.ObjectModel
{
public class News
{
public int id { get; set; }
public string title { get; set; }
public string slug { get; set; }
public string type { get; set; }
public string intro { get; set; }
public object image_display { get; set; }
public object mobile_image_display { get; set; }
public object image_preview { get; set; }
public object mobile_image_preview { get; set; }
public DateTime date { get; set; }
}
}
@@ -0,0 +1,16 @@
using System.Collections.Generic;
using System.Text;
namespace RgDevBot.ObjectModel
{
// Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse);
public class NewsListResponse
{
public int count { get; set; }
public string next { get; set; }
public object previous { get; set; }
public List<News> results { get; set; }
}
}
+19
View File
@@ -0,0 +1,19 @@
using System;
using System.Threading;
namespace RgDevBot
{
class Program
{
static void Main(string[] args)
{
var bot = new TelegramBot();
var parser = new NewsParser(bot);
while (true)
{
parser.Parse();
Thread.Sleep(TimeSpan.FromSeconds(1));
}
}
}
}
+13
View File
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="Telegram.Bot" Version="10.4.0" />
</ItemGroup>
</Project>
+30
View File
@@ -0,0 +1,30 @@
using System;
using Telegram.Bot;
namespace RgDevBot
{
public class TelegramBot
{
private readonly TelegramBotClient _telegram;
public string Token => "1479535614:AAFqHdsUFikzOti0zknK_pN2sEONNAXecPY";
public long MainChatId;
public TelegramBot(long chatId = -415825856)
{
MainChatId = chatId;
_telegram = new TelegramBotClient(Token);
_telegram.OnMessage += Bot_OnMessage;
_telegram.StartReceiving();
}
public void Bot_OnMessage(object sender, Telegram.Bot.Args.MessageEventArgs e)
{
Console.WriteLine($"[{e.Message.Chat.Id}, user: {e.Message.From.Id}] @{e.Message.From?.Username}: {e.Message.Text}");
}
public void SendMessage(string message, long? chatId = null)
{
_telegram.SendTextMessageAsync(chatId ?? MainChatId, message).GetAwaiter().GetResult();
}
}
}