How can i receive commands in my bot? Every command (like /start) are receiving as message. Of course i may check the firs char in message text, and after that realize what a command is received. But may be easier way exists?
Example (c#):
public static void StartListening()
{
var receiverOptions = new ReceiverOptions
{
AllowedUpdates = new UpdateType[]
{
// UpdateType.Message, or everything - does not matter
}
};
TelegramClient.StartReceiving(
updateHandler: SomeActivityAsync,
pollingErrorHandler: HandleErrorAsync,
receiverOptions: receiverOptions);
}
private static async Task SomeActivityAsync(ITelegramBotClient botClient, Update update, CancellationToken cancellationToken)
{
if (update.Type == UpdateType.Message)
Console.WriteLine("UpdateType.Message");
else
Console.WriteLine("Not UpdateType.Message");
}
And when i send /start or /any_other_registered_command, i see in console UpdateType.Message
I have a c# WebApi project in which the users can make orders in a website, each order after payment complete will execute a function called ConfirmOrder which will update the order status from STB to COMPLETED.
In the following function that looks like this:
public static void ConfirmOrder(string piva, string orderID, double importo = 0, string transazione = "", string paymentID = "", string tipo = "MENU")
{
string connectionString = getConnectionString(piva);
using var connection = new MySqlConnection(connectionString);
string query_menu = "QUERY";
string query_pagamenti = "QUERY";
using var cmd = new MySqlCommand(query_pagamenti, connection);
connection.Open();
cmd.Parameters.AddWithValue("#tipo", tipo.ToUpper());
cmd.Parameters.AddWithValue("#importo", importo);
cmd.Parameters.AddWithValue("#transazione", transazione);
cmd.Parameters.AddWithValue("#dataOra", DateTime.Now);
cmd.Parameters.AddWithValue("#orderID", orderID);
cmd.Parameters.AddWithValue("#paymentID", paymentID);
cmd.Prepare();
cmd.ExecuteNonQuery();
cmd.CommandText = query_menu;
cmd.ExecuteNonQuery();
if (!tipo.Equals("MENU"))
{
EmailHelper.SendRiepilogo(piva, int.Parse(orderID)); // SENDING SUMMARY MAIL
}
}
I'm calling another function SendRiepilogo which sends a summary to the user and the shop, but in this case i can't wait for that function response but it have to be executed for it's own without stucking ConfirmOrder callback.. so i can't wait for SendRiepilogo to be executed, at this point i've read about IHostingService, but i can't figure out on how i could migrate my SendRiepilogo to a IHostingService and run it from ConfirmOrder...
My SendRiepilogo looks like this:
public static async void SendRiepilogo(string piva, int idOrdine)
{
var order = GetOrdine(piva, idOrdine);
if (order == null)
{
return;
}
try
{
var negozio = getNegozio(order.idNegozio);
var from = new MailAddress("ordini#visualorder.it", "VisualOrder");
var to = new MailAddress(order.cliente.FirstOrDefault().email);
using MemoryStream ms = new MemoryStream();
QRCodeGenerator qrGenerator = new QRCodeGenerator();
QRCodeData qrCodeData = qrGenerator.CreateQrCode("vo/" + idOrdine, QRCodeGenerator.ECCLevel.Q);
Base64QRCode qrCode = new Base64QRCode(qrCodeData);
byte[] byteQr = Convert.FromBase64String(qrCode.GetGraphic(20));
MemoryStream streamQr = new MemoryStream(byteQr);
var qrImage = new LinkedResource(streamQr, MediaTypeNames.Image.Jpeg)
{
ContentId = "qrImage"
};
string nome = order.cliente.FirstOrDefault().nome;
var orderEmail = new { idOrdine, order, nome, negozio };
byte[] byteLogo = Convert.FromBase64String(System.Text.Encoding.UTF8.GetString(negozio.logo));
MemoryStream streamLogo = new MemoryStream(byteLogo);
var logoImage = new LinkedResource(streamLogo, MediaTypeNames.Image.Jpeg)
{
ContentId = "logoImage"
};
string template = File.ReadAllText("Views/Emails/EmailRiepilogo.cshtml");
var htmlBody = Engine.Razor.RunCompile(template, "riepilogo", null, orderEmail);
AlternateView alternateView = AlternateView.CreateAlternateViewFromString(htmlBody, null, MediaTypeNames.Text.Html);
alternateView.LinkedResources.Add(qrImage);
alternateView.LinkedResources.Add(logoImage);
var message = new MailMessage(from, to)
{
Subject = "Riepilogo ordine",
Body = htmlBody
};
message.IsBodyHtml = true;
message.AlternateViews.Add(alternateView);
using var smtp = new SmtpClient("smtps.aruba.it", 587)
{
EnableSsl = true,
Credentials = new NetworkCredential("XXX", "XXX")
};
await smtp.SendMailAsync(message); // sending email to user
await smtp.SendMailAsync(MessageNegozio(order, idOrdine, negozio)); // sending email to shop
}
catch (Exception e)
{
return;
}
ConfirmEmail(piva, idOrdine); // setting "EMAIL SENT" flag in DB to true
return;
}
A background (hosted) service is a completely different service, using its own thread to do its job. You can't have your controller "run" something on that service, you have to tell it what to do, and have it do it.
The Background tasks with hosted services section in the docs shows two different ways a long running background service can work :
A timed service can run each time a timer fires and do a periodic job, as long as the application is running
A queued service waits for messages in a queue and performs a job when a message arrives
Sending an email fits into the second case. You could use the documentation example almost as-is. You can create an IBackgroundTaskQueue interface that clients like your controller can use to submit jobs to run in the background:
public interface IBackgroundTaskQueue
{
void QueueBackgroundWorkItem(Func<CancellationToken, Task> workItem);
Task<Func<CancellationToken, Task>> DequeueAsync(
CancellationToken cancellationToken);
}
This interface can be added as a dependency in your container's constructor.
Assuming the injected service is called myJobQueue, the controller can enqueue a job to run in the background with :
IBackgroundTaskQueue _myJobQueue
public MyController(IBackgroundTaskQueue myJobQueue)
{
_myJobQueue=myJobQueue;
}
public void ConfirmOrder(...)
{
...
if (!tipo.Equals("MENU"))
{
var ordId=int.Parse(orderID);
_myJobQueue.QueueBackgroundWorkItem(ct=>EmailHelper.SendRiepilogoAsync(piva,ordId ));
}
async void should only be used for asynchronous event handlers. That's not what SendRiepilogo is. async void methods can't be awaited, they are essentially fire-and-forget methods that may never run, as the application doesn't know it has to await them. The correct syntax should be :
public static async Task SendRiepilogoAsync(string piva, int idOrdine)
{
...
}
The rest of the documentation example can be used as-is.
Simplifying the service
Instead of a generic queued service that runs any available job, you could create a queue that accepts specific message classes only, only an address and order ID, and have the service do the job of retrieving any data and sending the email. Essentially, SendRiepilogoAsync becomes part of the background service. This allows creating services that could eg batch emails, send several emails concurrently, apply throttling etc.
This would allow reusing expensive resources or perform expensive operations just once, eg create the SmptClient and authenticate before starting to process queue messages
I'm currently working on a Discord bot written in C#. It is supposed to celebrate the birthday of the users in a server, once they have provided their day and month of birth by sending a message (user-birthday pairs are stored inside a local .txt file). A birthday is celebrated by sending an embed in the main channel of the guild (with which it shares the ID parameter) if the date of execution happens to be someone's birthday.
The content of the Ready event handler of the DiscordSocketClient element within the Program class and a partial call stack are shown below:
public DiscordSocketClient Client;
//...
private async Task Client_Ready()
{
await Commands.CelebrateUsers(Client);
}
also
public static async Task CelebrateUsers(DiscordSocketClient client)
{
DateTime now = DateTime.Now;
List<ulong> users = new List<ulong>();
using (StreamReader reader = new StreamReader(#"docs\birthdays.txt"))
{
string date;
ulong user;
while ((date = reader.ReadLine()) != null)
{
user = Convert.ToUInt64(reader.ReadLine());
int[] dm = date.Split(' ').Select(x=>Convert.ToInt32(x)).ToArray();
if(now.Day == dm[0] && now.Month == dm[1])
{
foreach(SocketGuild g in client.Guilds)
{
if(g.Users.FirstOrDefault(x=>x.Id == user) != null)
await CelebrateUser(user, g);
}
}
}
}
}
private static async Task CelebrateUser(ulong id, SocketGuild guild)
{
var embed = new EmbedBuilder()
{
Title = "Auguri ",
Color = Discord.Color.Green,
ThumbnailUrl = "https://c7.uihere.com/files/240/249/186/hot-air-balloon-birthday-balloon.jpg",
ImageUrl = guild.GetUser(id).GetAvatarUrl(size:900),
Footer =
{
IconUrl = "https://cdn.discordapp.com/avatars/406195350903193600/9629b569007720204f7fe775a9be0aeb.png",
Text = "Generato da DrKikkoCeccato++."
},
Timestamp = DateTimeOffset.Now
};
await guild.DefaultChannel.SendMessageAsync("",embed:embed.Build());
}
However, I have an issue with the last function. The last piece of code (the "SendMessageAsync" part) is not even called.
Upon the initialization of the embed, the Log event of the DiscordSocketClient is fired with the following records:
"A Ready handler is blocking the gateway task."
"A Ready handler has thrown an unhandled exception."
The state changes to "Ready" immediately afterwards. Needless to say, my embed is not being sent.
I don't get what the problem might be, since every action is marked as "asynchronous" (in order to avoid problems with the 3-second timeout of the "Ready" event). Could anyone help me?
Thanks in advance.
I found two ways to send messages into service bus topic from azure function.
one is using output -
[FunctionName("ServiceBusOutput")]
[return: ServiceBus("myqueue", Connection = "ServiceBusConnection")]
public static string ServiceBusOutput([HttpTrigger] dynamic input, ILogger log)
{
log.LogInformation($"C# function processed: {input.Text}");
return input.Text;
}
Another is using code -
const string ServiceBusConnectionString = "string";
const string TopicName = "topicName";
static ITopicClient topicClient;
topicClient = new TopicClient(ServiceBusConnectionString, TopicName);
string messageBody = "Test";
var message = new Message(Encoding.UTF8.GetBytes(messageBody));
await topicClient.SendAsync(message);
I'm not getting which one we should use and when?
if we use Output how to pass queue name myqueue as a variable
so that in code we can assign it.
if i have array how can we return one by one message to output
return which will send one by one message to queue ?
Full examples from here. For instance, how to write multiple messages, using the ICollector
public static void Run(TimerInfo myTimer, ILogger log, [ServiceBus("myqueue", Connection = "ServiceBusConnection")] ICollector<string> outputSbQueue)
{
string message = $"Service Bus queue messages created at: {DateTime.Now}";
log.LogInformation(message);
outputSbQueue.Add("1 " + message);
outputSbQueue.Add("2 " + message);
}
As far as I know the first version, using return does not work if you have any async calls inside your Function. The version using the collector can also work in async Functions but simply using an IAsyncCollector instead.
if we use Output how to pass queue name myqueue as a variable
so that in code we can assign it.
For this you can use Imperative Binding.Imperative binding is useful when binding parameters need to be computed at runtime rather than design time. More reference here
Example:
public static async Task ServiceBusBinderTest(
string message,
int numMessages,
Binder binder) {
var attribute = new ServiceBusAttribute(BinderQueueName) {
EntityType = EntityType.Queue
};
var collector = await binder.BindAsync < IAsyncCollector < string >> (attribute);
for (int i = 0; i < numMessages; i++) {
await collector.AddAsync(message + i);
}
await collector.FlushAsync();
}
if i have array how can we return one by one message to output return which will send one by one message to queue ?
You can configure the OnMessageOptions instance with decreasing your MaxConcurrentCalls
OnMessageOptions options = new OnMessageOptions();
options.AutoComplete = false;
options.MaxConcurrentCalls = 5;
I want to create template for push notification so far I'm stuck here and it's not working.
public static async void SendPushNotificationApns1(string msgTemplate)
{
NotificationHubClient hub = NotificationHubClient.CreateClientFromConnectionString(ListenConnectionString, NotificationHubName);
string msg = "{\"aps\":{\"alert\":\"$(msgTemplate)\"}}";
await hub.SendAppleNativeNotificationAsync(msg);
}
It happened with: string msg = "{\"aps\":{\"alert\":\"" + $"{messageParam}" +"\"}}";