NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

{
"ConnectionStrings": {
"DefaultConnection": "Server=your_server;Database=your_database;User Id=your_user;Password=your_password;"
},
"Gupshup": {
"ApiKey": "your_gupshup_api_key",
"BaseUrl": "https://api.gupshup.io/sm/api/v1/msg"
}
}




using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json;

namespace RFQTradeAPI.Services
{
public class GupshupService
{
private readonly HttpClient _httpClient;
private readonly string _apiKey;
private readonly string _baseUrl;

public GupshupService(HttpClient httpClient, IConfiguration configuration)
{
_httpClient = httpClient;
_apiKey = configuration["Gupshup:ApiKey"];
_baseUrl = configuration["Gupshup:BaseUrl"];
}

public async Task SendMessageAsync(string phoneNumber, string message)
{
var payload = new
{
channel = "whatsapp",
source = "your_gupshup_source_number",
destination = phoneNumber,
message = new
{
type = "text",
text = message
}
};

var jsonPayload = JsonConvert.SerializeObject(payload);
var content = new StringContent(jsonPayload, Encoding.UTF8, "application/json");

_httpClient.DefaultRequestHeaders.Add("apikey", _apiKey);
var response = await _httpClient.PostAsync(_baseUrl, content);
response.EnsureSuccessStatusCode();
}
}
}








using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using RFQTradeAPI.Data;
using RFQTradeAPI.Services;

namespace RFQTradeAPI.HostedServices
{
public class TradeStatusChecker : BackgroundService
{
private readonly IServiceProvider _serviceProvider;
private readonly ILogger<TradeStatusChecker> _logger;

public TradeStatusChecker(IServiceProvider serviceProvider, ILogger<TradeStatusChecker> logger)
{
_serviceProvider = serviceProvider;
_logger = logger;
}

protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
while (!stoppingToken.IsCancellationRequested)
{
using (var scope = _serviceProvider.CreateScope())
{
var context = scope.ServiceProvider.GetRequiredService<RfqTradeContext>();
var gupshupService = scope.ServiceProvider.GetRequiredService<GupshupService>();

var trades = await context.RfqTradeDetails
.Where(t => t.Status == "transact")
.ToListAsync(stoppingToken);

foreach (var trade in trades)
{
try
{
await gupshupService.SendMessageAsync("recipient_phone_number", "Transaction message");
// Update the status or any other required actions
trade.Status = "processed";
context.RfqTradeDetails.Update(trade);
await context.SaveChangesAsync(stoppingToken);
}
catch (Exception ex)
{
_logger.LogError(ex, "Error sending message for trade ID {TradeId}", trade.Id);
}
}
}

await Task.Delay(TimeSpan.FromMinutes(1), stoppingToken); // Check every minute
}
}
}
}




program.cs

using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using RFQTradeAPI.Data;
using RFQTradeAPI.HostedServices;
using RFQTradeAPI.Services;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddDbContext<RfqTradeContext>(options =>
options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));

builder.Services.AddHttpClient<GupshupService>();
builder.Services.AddHostedService<TradeStatusChecker>();

builder.Services.AddControllers();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
app.UseHsts();
}

app.UseHttpsRedirection();
app.UseRouting();

app.UseAuthorization();

app.MapControllers();

app.Run();
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.