-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from AdityaP700/feature/mongodb-integration
Feature/mongodb integration
- Loading branch information
Showing
13 changed files
with
422 additions
and
77 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -149,6 +149,12 @@ public async Task<IActionResult> GetSeriousTickets() | |
} | ||
} | ||
|
||
[HttpGet("test")] | ||
public IActionResult Test() | ||
{ | ||
return Ok(new { message = "API is working!" }); | ||
} | ||
|
||
private string ConvertHtmlToPlainText(string html) | ||
{ | ||
if (string.IsNullOrEmpty(html)) return string.Empty; | ||
|
@@ -189,5 +195,117 @@ private async Task StoreEmail(ProcessedEmail email) | |
string updatedJson = JsonConvert.SerializeObject(emails, Formatting.Indented); | ||
await System.IO.File.WriteAllTextAsync(filePath, updatedJson); | ||
} | ||
|
||
[HttpPost("trubot")] | ||
[Consumes("application/x-www-form-urlencoded")] | ||
public async Task<IActionResult> ProcessTrubotEmail([FromForm] TrubotEmailRequest emailData) | ||
{ | ||
try | ||
{ | ||
if (emailData == null) | ||
{ | ||
return BadRequest(new { message = "Email data is required" }); | ||
} | ||
|
||
_logger.LogInformation($"Processing TruBot email from: {emailData.senderEmail}"); | ||
|
||
// Create EmailData object from TruBot request | ||
var email = new EmailData | ||
{ | ||
Subject = emailData.subject ?? string.Empty, | ||
Body = emailData.body ?? string.Empty, | ||
SenderEmail = emailData.senderEmail ?? string.Empty, | ||
ReceiverEmail = "[email protected]", | ||
Score = 0, // Will be updated by sentiment analysis | ||
Type = "pending", | ||
ReceivedDate = DateTime.UtcNow | ||
}; | ||
|
||
// Process the email using existing logic | ||
var plainTextBody = ConvertHtmlToPlainText(email.Body); | ||
|
||
// Analyze sentiment using Groq | ||
int sentimentScore = await _groqService.AnalyzeSentiment(plainTextBody); | ||
|
||
// Update email with sentiment score and type | ||
email.Score = sentimentScore; | ||
email.Type = sentimentScore > 60 ? "negative" : "positive"; | ||
|
||
// Log before storing | ||
_logger.LogInformation($"Storing email: Subject={email.Subject}, Sender={email.SenderEmail}, Score={email.Score}"); | ||
|
||
// Store in MongoDB | ||
await _emailService.CreateAsync(new Email | ||
{ | ||
Body = plainTextBody, | ||
Score = sentimentScore, | ||
Sender = email.SenderEmail, | ||
Receiver = email.ReceiverEmail, | ||
Type = email.Type, | ||
Time = DateTime.UtcNow | ||
}); | ||
|
||
// If sentiment score is high (negative), notify connected clients | ||
if (sentimentScore > 60) | ||
{ | ||
await _hubContext.Clients.All.SendAsync("ReceiveSeriousTicket", email); | ||
} | ||
|
||
return Ok(new | ||
{ | ||
status = "Success", | ||
message = "Email processed successfully", | ||
sentimentScore = sentimentScore, | ||
type = email.Type | ||
}); | ||
} | ||
catch (Exception ex) | ||
{ | ||
_logger.LogError($"Error processing TruBot email: {ex.Message}"); | ||
return StatusCode(500, new { message = "Failed to process email", error = ex.Message }); | ||
} | ||
} | ||
|
||
[HttpPost("test-trubot")] | ||
[Consumes("application/x-www-form-urlencoded")] | ||
public IActionResult TestTrubotEmail([FromForm] TrubotEmailRequest emailData) | ||
{ | ||
try | ||
{ | ||
_logger.LogInformation("=== TEST ENDPOINT - Received TruBot Data ==="); | ||
_logger.LogInformation($"Subject: {emailData.subject}"); | ||
_logger.LogInformation($"Body: {emailData.body}"); | ||
_logger.LogInformation($"Sender Email: {emailData.senderEmail}"); | ||
_logger.LogInformation("=== RAW FORM DATA ==="); | ||
|
||
// Log all form data received | ||
foreach (var key in Request.Form.Keys) | ||
{ | ||
_logger.LogInformation($"{key}: {Request.Form[key]}"); | ||
} | ||
|
||
return Ok(new | ||
{ | ||
message = "Test endpoint - Data received", | ||
receivedData = new | ||
{ | ||
subject = emailData.subject, | ||
body = emailData.body, | ||
senderEmail = emailData.senderEmail | ||
}, | ||
rawFormData = Request.Form.ToDictionary(x => x.Key, x => x.Value.ToString()) | ||
}); | ||
} | ||
catch (Exception ex) | ||
{ | ||
_logger.LogError($"Error in test endpoint: {ex.Message}"); | ||
return StatusCode(500, new | ||
{ | ||
message = "Error in test endpoint", | ||
error = ex.Message, | ||
stackTrace = ex.StackTrace | ||
}); | ||
} | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
namespace SentimatrixAPI.Models | ||
{ | ||
public class TrubotEmailRequest | ||
{ | ||
public string? subject { get; set; } | ||
public string? body { get; set; } | ||
public string? senderEmail { get; set; } | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"profiles": { | ||
"SentimatrixAPI": { | ||
"commandName": "Project", | ||
"dotnetRunMessages": true, | ||
"launchBrowser": true, | ||
"launchUrl": "swagger", | ||
"applicationUrl": "http://localhost:5000", | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
} | ||
} | ||
} | ||
} |
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 was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,48 @@ | ||
using System.Collections.ObjectModel; | ||
using System.Windows; | ||
using System.Net.Http; | ||
using Newtonsoft.Json; | ||
using System.Threading.Tasks; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using WpfSidebarApp.Models; | ||
|
||
namespace WpfSidebarApp | ||
{ | ||
public partial class AllEmailsWindow : Window | ||
{ | ||
private readonly HttpClient _httpClient; | ||
private readonly string _apiBaseUrl = "http://localhost:5000/api"; | ||
public ObservableCollection<Email> Emails { get; set; } | ||
|
||
public AllEmailsWindow() | ||
{ | ||
InitializeComponent(); | ||
LoadEmails(); | ||
EmailsGrid.ItemsSource = Emails; | ||
_httpClient = new HttpClient(); | ||
Emails = new ObservableCollection<Email>(); | ||
LoadEmailsAsync(); | ||
} | ||
|
||
private void LoadEmails() | ||
private async Task LoadEmailsAsync() | ||
{ | ||
// Sample data for demonstration | ||
Emails = new ObservableCollection<Email> | ||
try | ||
{ | ||
new Email { Sender = "[email protected]", Receiver = "[email protected]", Score = 5, Body = "This is a sample email body 1." }, | ||
new Email { Sender = "[email protected]", Receiver = "[email protected]", Score = 3, Body = "This is a sample email body 2." }, | ||
new Email { Sender = "[email protected]", Receiver = "[email protected]", Score = 4, Body = "This is a sample email body 3." } | ||
}; | ||
var response = await _httpClient.GetStringAsync($"{_apiBaseUrl}/email"); | ||
var emailList = JsonConvert.DeserializeObject<List<Email>>(response); | ||
|
||
Emails.Clear(); | ||
foreach (var email in emailList.OrderByDescending(e => e.Time)) | ||
{ | ||
Emails.Add(email); | ||
} | ||
|
||
EmailsGrid.ItemsSource = Emails; | ||
} | ||
catch (Exception ex) | ||
{ | ||
MessageBox.Show($"Error loading emails: {ex.Message}", "Error", MessageBoxButton.OK, MessageBoxImage.Error); | ||
} | ||
} | ||
|
||
private void DashboardButton_Click(object sender, RoutedEventArgs e) | ||
|
Oops, something went wrong.