forked from jorgevgut/airquality-mx
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Fixed typos on environment variables - Using Twitter library asynchronously - Updating rules for notifying, tweets must be 30 minutes apart. - Fixing minor bugs in feed processing, adding null streams check TODOS: tests, strong validation, error handling. Automation
- Loading branch information
Jorge A Villalobos G (aka Latincoder)
committed
Apr 21, 2020
1 parent
67f1f8a
commit 9f2daa9
Showing
4 changed files
with
57 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 46 additions & 20 deletions
66
src/Aws/AirQualityTwitterPublisherLambda/src/AirQualityTwitterPublisherLambda/Function.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,72 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Amazon.Lambda.SNSEvents; | ||
using Tweetinvi; | ||
using Tweetinvi.Models; | ||
|
||
using Amazon.Lambda.Core; | ||
|
||
|
||
// Assembly attribute to enable the Lambda function's JSON input to be converted into a .NET class. | ||
//[assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.SystemTextJson.LambdaJsonSerializer))] | ||
[assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.SystemTextJson.LambdaJsonSerializer))] | ||
|
||
namespace AirQualityTwitterPublisherLambda | ||
{ | ||
public class Function | ||
{ | ||
|
||
private bool hasMessage(SNSEvent.SNSRecord snsRecord) { | ||
var msg = string.Empty; | ||
if (snsRecord == null) return false; | ||
try { | ||
msg = snsRecord.Sns.Message; | ||
} catch(NullReferenceException e) { | ||
return false; | ||
} | ||
return !string.IsNullOrEmpty(msg); | ||
} | ||
/// <summary> | ||
/// A simple function that takes a string and does a ToUpper | ||
/// </summary> | ||
/// <param name="input"></param> | ||
/// <param name="context"></param> | ||
/// <returns></returns> | ||
public void FunctionHandler(System.IO.Stream inputStream, ILambdaContext context) | ||
public System.Threading.Tasks.Task<string> FunctionHandler(SNSEvent snsEvent, ILambdaContext context) | ||
{ | ||
System.IO.StreamReader reader = new System.IO.StreamReader(inputStream); | ||
var receivedMessage = reader.ReadToEnd(); | ||
System.Console.WriteLine("This is where twitter API integration will happen"); | ||
System.Console.WriteLine(receivedMessage); | ||
var apiKey = Environment.GetEnvironmentVariable("TWITTER_API_KEY"); | ||
var apiSecret = Environment.GetEnvironmentVariable("TWITTER_API_SECRET"); | ||
var apiKey = Environment.GetEnvironmentVariable("TWITTER_APIK"); | ||
var apiSecret = Environment.GetEnvironmentVariable("TWITTER_APIS"); | ||
var token = Environment.GetEnvironmentVariable("TWITTER_ACCESS_TOKEN"); | ||
var tokenSecret = Environment.GetEnvironmentVariable("TWITTER_TOKEN_SECRET"); | ||
var credentials = new TwitterCredentials(apiKey, apiSecret, token, tokenSecret); | ||
Auth.SetCredentials(credentials); | ||
var tokenSecret = Environment.GetEnvironmentVariable("TWITTER_SECRET_TOKEN"); | ||
var credentials = Auth.CreateCredentials(apiKey, apiSecret, token, tokenSecret); | ||
Auth.SetUserCredentials(apiKey, apiSecret, token, tokenSecret); | ||
// TweetInvi lacks returning asynchronous types that can be awaited | ||
var publisher = User.GetAuthenticatedUser(credentials); | ||
|
||
try { | ||
var tweet = Auth.ExecuteOperationWithCredentials(credentials, () => Tweet.PublishTweet(receivedMessage)); | ||
System.Console.WriteLine($"Published tweet by {tweet.TweetDTO.CreatedBy.Name}"); | ||
} catch(Exception error) { | ||
// TODO: if tweet fails publish to DLQ (SQS) | ||
System.Console.WriteLine(error); | ||
} | ||
var t = new System.Threading.Tasks.Task<string>(() => { | ||
System.Console.WriteLine($"Authenticated User {publisher.Name}"); | ||
var records = from r in snsEvent.Records | ||
where hasMessage(r) | ||
select r; | ||
foreach(var snsRecord in records) { | ||
var receivedMessage = snsRecord.Sns.Message; | ||
System.Console.WriteLine("This is where twitter API integration will happen"); | ||
System.Console.WriteLine(receivedMessage); | ||
try { | ||
System.Console.WriteLine($"Start to publish Tweet.{receivedMessage}"); | ||
publisher.PublishTweet(receivedMessage); | ||
System.Threading.Thread.Sleep(5000); | ||
System.Console.WriteLine($"Published tweet: {publisher.Name}"); | ||
} catch(Exception error) { | ||
// TODO: if tweet fails publish to DLQ (SQS) | ||
System.Console.WriteLine($"Error detected:{error}"); | ||
} | ||
} | ||
return "{\"msg\":\"execution completed\"}"; | ||
}); | ||
t.RunSynchronously(); | ||
return t; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters