Posted January 11, 2018 by Troll Purse
#dev #code
Recently, Troll Purse setup a public invite for the Troll Purse Discord Server. And, as with all things, we decided to test out using Discord Webhooks to push updates to our members in realtime. This is by far the most effective realtime pushing we have conceived yet. It was so easy, sharing it will be just as easy.
Usually, the pattern at Troll Purse to push to third party accounts follows this pattern:
This time, we decided to skip step 3. For the most part, the developers at Troll Purse recognized that this push would require very little data transformation and authentication routines. In fact, all of the work was done in one POST
request to the Troll Purse Discord Server.
public async Task<string> FunctionHandler(SNSEvent input, ILambdaContext context) { try { var messageJSONString = input.Records[0]?.Sns.Message; context?.Logger.LogLine($"Received({input.Records[0]?.Sns.MessageId}): {messageJSONString}"); if (messageJSONString != null) { var messageContent = JsonConvert.DeserializeObject<BlogContentUpdated>(messageJSONString); using (var httpClient = new HttpClient()) { string payload = $"{"content":"{messageContent.PostTitle}. {messageContent.ContentSnippet}... {messageContent.PostLink}"}"; var response = await httpClient.PostAsync(Environment.GetEnvironmentVariable("discord_webhook"), new StringContent(payloadEncoding.UTF8, "application/json")); return response.StatusCode.ToString(); } } else { return null; } } catch (Exception e) { context?.Logger.LogLine("Unable to Discord the SNS message"); context?.Logger.LogLine(e.Message); context?.Logger.LogLine(e.StackTrace); return null; } }
Notes:
BlogContentUpdated
is code defined in an external Troll Purse binary.All of these features that Troll Purse has blogged about are done within a few hours. This is easily aided by the idea of serverless programming. There is no overhead of provisioning servers, testing different server environments, and configuring a network for these functions. It removes a lot of network infrastructure and enables Troll Purse developers to create fast, reactive, internal services.
Please, if you spend too much time configuring and setting up, try using AWS Lambda to speed up development time.
In two lines, without a library or API wrapper, our developers can now push blog updates to our Discord server. This is a nice quick feature that we plan on integrating in our automated build environment to push updates about new versions released to the public. Enjoy!