Commit 74030888 authored by Robert Rudman's avatar Robert Rudman

Return message ID when sending messages. Add functionality to pin items. Add...

Return message ID when sending messages. Add functionality to pin items. Add service to start burns via Gemmy
parent 5d2c9469
Pipeline #42112673 passed with stage
in 3 minutes and 40 seconds
......@@ -23,8 +23,7 @@ namespace Rodgort.Services
{
public class BurnakiFollowService : IHostedService
{
private const int Headquarters = 185585;
private const int RobUserId = 563532;
public const int RobUserId = 563532;
private readonly IServiceProvider _serviceProvider;
private readonly ILogger<BurnakiFollowService> _logger;
......@@ -55,9 +54,9 @@ namespace Rodgort.Services
foreach (var burnakiFollow in burnakiFollows)
FollowInRoom(burnakiFollow.RoomId, burnakiFollow.BurnakiId, burnakiFollow.FollowStarted, burnakiFollow.Tag, dateService, cancellationToken);
var events = chatClient.SubscribeToEvents(ChatSite.StackOverflow, Headquarters);
var events = chatClient.SubscribeToEvents(ChatSite.StackOverflow, ChatRooms.HEADQUARTERS);
await events.FirstAsync();
chatClient.SendMessage(ChatSite.StackOverflow, Headquarters, "o/");
chatClient.SendMessage(ChatSite.StackOverflow, ChatRooms.HEADQUARTERS, "o/");
_logger.LogInformation("Successfully joined headquarters");
await events
.ReplyAlive()
......@@ -89,7 +88,7 @@ namespace Rodgort.Services
var events = chatClient.SubscribeToEvents(ChatSite.StackOverflow, roomId);
await events.FirstAsync();
_logger.LogInformation($"Successfully joined room {roomId}");
chatClient.SendMessage(ChatSite.StackOverflow, Headquarters, $"I just joined {roomId}");
chatClient.SendMessage(ChatSite.StackOverflow, ChatRooms.HEADQUARTERS, $"I just joined {roomId}");
await events
.ReplyAlive()
.OnlyMessages()
......@@ -148,7 +147,7 @@ namespace Rodgort.Services
}
foreach (var message in messages)
await chatClient.SendMessage(ChatSite.StackOverflow, Headquarters, message);
await chatClient.SendMessage(ChatSite.StackOverflow, ChatRooms.HEADQUARTERS, message);
var apiClient = _serviceProvider.GetRequiredService<ApiClient>();
var revisions = await apiClient.Revisions("stackoverflow.com", questionIdList);
......
using System.Threading.Tasks;
using Rodgort.Utilities;
using StackExchangeChat;
namespace Rodgort.Services
{
public class NewBurninationService
{
private readonly ChatClient _chatClient;
public NewBurninationService(ChatClient chatClient)
{
_chatClient = chatClient;
}
public async Task CreateRoomForBurn(ChatSite chatSite, int currentRoomId, string tag, string metaPostUrl)
{
var roomName = $"Observation room for [{tag}] burnination";
var roomId = await _chatClient.CreateRoom(chatSite, currentRoomId, roomName, string.Empty);
var gemmyMessage = $"@Gemmy start tag [{tag}] {roomId} https://chat.stackoverflow.com/rooms/{roomId}";
await _chatClient.SendMessage(ChatSite.StackOverflow, ChatRooms.SO_BOTICS_WORKSHOP, gemmyMessage);
var burninationMessage = $"The burnination of [tag:{tag}] has begun! {metaPostUrl}";
var burninationMessageId = await _chatClient.SendMessage(ChatSite.StackOverflow, ChatRooms.SO_BOTICS_WORKSHOP, burninationMessage);
await _chatClient.PinMessage(ChatSite.StackOverflow, ChatRooms.SO_BOTICS_WORKSHOP, burninationMessageId);
}
}
}
namespace Rodgort.Utilities
{
public class ChatRooms
{
public const int HEADQUARTERS = 185585;
public const int SO_BOTICS_WORKSHOP = 167908;
}
}
......@@ -3,6 +3,8 @@ using System.Net;
using System.Net.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Rodgort.Services;
using Rodgort.Utilities;
using StackExchangeChat.Utilities;
namespace StackExchangeChat.Console
......@@ -27,7 +29,8 @@ namespace StackExchangeChat.Console
serviceCollection.AddScoped<SiteAuthenticator>();
serviceCollection.AddScoped<ChatClient>();
serviceCollection.AddScoped<NewBurninationService>();
serviceCollection.AddTransient(_ => new HttpClientWithHandler(new HttpClientHandler { AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate }));
serviceCollection.AddSingleton(_ => config);
......@@ -46,7 +49,11 @@ namespace StackExchangeChat.Console
// var result = apiThing.TotalQuestionsByTag("design").GetAwaiter().GetResult();
var chatClient = serviceProvider.GetService<ChatClient>();
var roomId = chatClient.CreateRoom(ChatSite.StackOverflow, 167908, "This is a testing room", "This is a testing description").GetAwaiter().GetResult();
var newBurninationService = serviceProvider.GetService<NewBurninationService>();
newBurninationService.CreateRoomForBurn(ChatSite.StackOverflow, ChatRooms.SO_BOTICS_WORKSHOP, "priority", "https://meta.stackoverflow.com/questions/285084/should-we-burninate-the-priority-tag").GetAwaiter().GetResult();
// var roomId = chatClient.CreateRoom(ChatSite.StackOverflow, 167908, "This is a testing room", "This is a testing description").GetAwaiter().GetResult();
//var events = chatClient.SubscribeToEvents(ChatSite.StackExchange, 86421);
//events.Subscribe(System.Console.WriteLine);
......
......@@ -15,6 +15,7 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Rodgort\Rodgort.csproj" />
<ProjectReference Include="..\StackExchangeApi\StackExchangeApi.csproj" />
<ProjectReference Include="..\StackExchangeChat\StackExchangeChat.csproj" />
</ItemGroup>
......
......@@ -26,20 +26,25 @@ namespace StackExchangeChat
_httpClient = httpClient;
}
public async Task SendMessage(ChatSite chatSite, int roomId, string message)
public async Task<int> SendMessage(ChatSite chatSite, int roomId, string message)
{
await ThrottlingUtils.Throttle(ChatThrottleGroups.WebRequestThrottle, async () =>
return await ThrottlingUtils.Throttle<int>(ChatThrottleGroups.WebRequestThrottle, async () =>
{
var fkey = (await _siteAuthenticator.GetRoomDetails(chatSite, roomId)).FKey;
await _siteAuthenticator.AuthenticateClient(_httpClient, chatSite);
await _httpClient.PostAsync($"https://{chatSite.ChatDomain}/chats/{roomId}/messages/new",
var response = await _httpClient.PostAsync($"https://{chatSite.ChatDomain}/chats/{roomId}/messages/new",
new FormUrlEncodedContent(
new Dictionary<string, string>
{
{"text", message},
{"fkey", fkey}
}));
}, Task.Delay(TimeSpan.FromSeconds(5)));
var responseString = await response.Content.ReadAsStringAsync();
var responsePayload = JsonConvert.DeserializeObject<JObject>(responseString);
return int.Parse(responsePayload["id"].ToString());
}, _ => Task.Delay(TimeSpan.FromSeconds(5)));
}
private readonly Regex _roomRegex = new Regex(@"\/rooms\/info\/(\d+)\/");
......@@ -67,6 +72,21 @@ namespace StackExchangeChat
}, _ => Task.Delay(TimeSpan.FromSeconds(5)));
}
public async Task PinMessage(ChatSite chatSite, int currentRoomId, int messageId)
{
await ThrottlingUtils.Throttle(ChatThrottleGroups.WebRequestThrottle, async () =>
{
var fkey = (await _siteAuthenticator.GetRoomDetails(chatSite, currentRoomId)).FKey;
await _siteAuthenticator.AuthenticateClient(_httpClient, chatSite);
await _httpClient.PostAsync($"https://{chatSite.ChatDomain}/messages/{messageId}/owner-star ",
new FormUrlEncodedContent(
new Dictionary<string, string>
{
{"fkey", fkey},
}));
}, Task.Delay(TimeSpan.FromSeconds(5)));
}
public IObservable<ChatEvent> SubscribeToEvents(ChatSite chatSite, int roomId)
{
return Observable.Create<ChatEvent>(async observer =>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment