1

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?

Septimius
  • 59
  • 8

2 Answers2

0

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

Mihail Feraru
  • 1,419
  • 9
  • 17
0

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.

lobstar
  • 310
  • 4
  • 8