skip to Main Content

I can’t just do this

public static void myBot_OnMessage(object sender, Telegram.Bot.Args.MessageEventArgs msg)
{
    if (DateTime.Now.Hour == 01)
    {
        myBot.SendTextMessageAsync(msg.Message.Chat.Id, "Good Night");
    }
}

Because that requires the user to first send a message at that exact time for them to receive the message. How can i make it autosend a message at a specific to anyone who ever started it without the person having to send message the exact time it would send the automatic message?

2

Answers


  1. You should schedule a task by using the provided interface by Windows.
    Also, you should store the ID’s of the users in a database and iterate over them every time the scheluded task is running.

    You can find more about task scheduling in C# here Creating Scheduled Tasks

    Login or Signup to reply.
  2. If you developing using .net core you can use IHostedService for realization background task. Also there exist a libraries for sheduled tasks. I recomend Hangfire.
    It’s free and easy for use. You can add task like this:

    var nextDay = DateTime.Now.AddDays(1);
    var sendDate = new DateTime(nextDay.Year, nextDay.Month, nextDay.Day, 1,0,0);
    var jobId = BackgroundJob.Schedule(
       () => myBot.SendTextMessageAsync(msg.Message.Chat.Id, "Good Night");
       (sendDate - DateTime.Now));
    

    You need database for using Hangfire.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search