This repository was archived by the owner on Sep 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCallSentimentAPIFromTweetCosmos.cs
More file actions
62 lines (52 loc) · 2.28 KB
/
CallSentimentAPIFromTweetCosmos.cs
File metadata and controls
62 lines (52 loc) · 2.28 KB
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
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using AIForGunSafetyFunctionApp.Handlers;
using AIForGunSafetyFunctionApp.Models;
using AIForGunSafetyFunctionApp.Services;
using Microsoft.Azure.Documents;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
namespace AIForGunSafetyFunctionApp
{
/// <summary>
/// Function app to call sentiment analysis
/// </summary>
public class CallSentimentAPIFromTweetCosmos
{
[FunctionName("CallSentimentAPIFromTweetCosmos")]
public async Task Run([CosmosDBTrigger(
databaseName: "temp-db",
collectionName: "tweets",
ConnectionStringSetting = "CosmosDBConnection",
LeaseCollectionName = "leases", CreateLeaseCollectionIfNotExists = true)]IReadOnlyList<Document> input,
ILogger log)
{
if (input != null && input.Count > 0)
{
SocialMediaPostHandler socialMediaPostHandler = new SocialMediaPostHandler();
TwitterService twitterService = new TwitterService();
foreach (Document document in input)
{
Tweet currentTweet = JsonConvert.DeserializeObject<Tweet>(document.GetPropertyValue<string>("Description"));
//Call the sentiment api by passing tweet.text and get the sentiment and score.
var sentimentResponse = await socialMediaPostHandler.GetSentimentScoreByText(currentTweet.text);
var twitterUser = await socialMediaPostHandler.GetTwitterUser(currentTweet.author_id);
if (twitterUser.data != null)
{
var userData = new UserData
{
TwitterUser = twitterUser.data[0],
SentimentScore = sentimentResponse.SentimentScore,
TweetText = currentTweet.text,
Location = currentTweet.geo
};
await twitterService.AddMessageToServiceBusQueue("twitteruserqueue", JsonConvert.SerializeObject(userData));
}
}
}
}
}
}