让bot在特定时间每天发送消息

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了让bot在特定时间每天发送消息相关的知识,希望对你有一定的参考价值。

我正在用Bot Framework构建一个机器人,应该在MS Teams运行,我希望它每天早上6:30给我发消息。

我有一个方法,每天在6:30在Program文件中调用。我有一个从机器人发送消息的方法。

这是我的计时器的代码:

private static Timer _timer;

    private static int count = 1;


    public static void Main(string[] args)
    {        
        //Initialization of _timer   
        _timer = new Timer(x => { callTimerMethod(); }, null, Timeout.Infinite, Timeout.Infinite);
        Setup_Timer();

        BuildWebHost(args).Run();
    }

    /// <summary>  
    /// This method will execute every day at 06:30.   
    /// </summary>  
    public static void callTimerMethod()
    {
        System.Diagnostics.Debug.WriteLine(string.Format("Method is called"));
        System.Diagnostics.Debug.Write(DateTime.Now.ToString("dddd, dd MMMM yyyy HH:mm:ss"));
        count = count + 1;
    }

    /// <summary>  
    /// This method will set the timer execution time and will change the   
    /// tick time of timer.
    /// </summary>  
    private static void Setup_Timer()
    {
        DateTime currentTime = DateTime.Now;
        DateTime timerRunningTime = new DateTime(currentTime.Year, currentTime.Month, currentTime.Day, 6, 30, 0);
        timerRunningTime = timerRunningTime.AddDays(1);

        double tickTime = (double)(timerRunningTime - DateTime.Now).TotalSeconds;

        _timer.Change(TimeSpan.FromSeconds(tickTime),
        TimeSpan.FromSeconds(tickTime));
    }

我要归档的是我想将callTimerMethod()的内容更改为此方法:

public async Task AlertSubscribers(ITurnContext turncontext, CancellationToken cancellationToken = default(CancellationToken))
    {
        using (var db = new DataBaseContext())
        {
            var msg = "";
            var today = DateTime.Today.ToString("dddd");
            var product = db linq code;

            foreach(var prod in product)
            {
                msg = $"Reminder! {prod.bla}";
            }

            // Get the conversation state from the turn context.
            var state = await _accessors.CounterState.GetAsync(turncontext, () => new CounterState());

            // Set the property using the accessor.
            await _accessors.CounterState.SetAsync(turncontext, state);

            // Save the new turn count into the conversation state.
            await _accessors.ConversationState.SaveChangesAsync(turncontext);

            // Echo back to the user whatever msg is.
            await turncontext.SendActivityAsync(msg);
        }
    }

但我找不到一种方法来存档它...真的很感激一些帮助,我已经搜索了很多但没有发现类似的问题。

问题是所有命名空间(例如ITurncontext,Conversationstate等等......)

希望能描述我的问题......

提前致谢!

编辑:

它不一定需要成为AlertSubscribers()方法,而是一个函数,只是代码执行类似的事情。

我已经尝试过这段代码,但是我无法让它让机器人向用户发送消息(在本例中我是在模拟器中):

public static void callTimerMethod()
    {
        IMessageActivity message = Activity.CreateMessageActivity();

        message.Text = "Hello!";
        message.TextFormat = "plain";
        message.Locale = "en-Us";
        message.ChannelId = "emulator";
        message.Id = "A guid";
        message.InputHint = "acceptingInput";
        message.LocalTimestamp = DateTimeOffset.Now;
        message.ReplyToId = "A guid";
        message.ServiceUrl = "http://localhost:50265";
        message.Timestamp = DateTimeOffset.Now;
        message.Type = "ConversationUpdate";

        message.AsConversationUpdateActivity();
    }

我是Bot框架的新手,所以我的代码和我的问题可能是错误的......

答案

解决了!

public static async void callTimerMethod()
{
    await ConversationStarter.Resume("conversationId", "emulator");
}

我不得不将callTimerMethod()更改为异步方法并创建一个ConversationStarter类来为我处理消息。

这是ConversationStarter

public class ConversationStarter
{
    public static string fromId;
    public static string fromName;
    public static string toId;
    public static string toName;
    public static string serviceUrl;
    public static string channelId;
    public static string conversationId;


    public static async Task Resume(string conversationId, string channelId)
    {
        conversationId = await Talk(conversationId, channelId, $"Hi there!");
        conversationId = await Talk(conversationId, channelId, $"This is a notification!");
    }

    private static async Task<string> Talk(string conversationId, string channelId, string msg)
    {
        var userAccount = new ChannelAccount(toId, toName);
        var botAccount = new ChannelAccount(fromId, fromName);
        var connector = new ConnectorClient(new Uri(serviceUrl));

        IMessageActivity message = Activity.CreateMessageActivity();
        if (!string.IsNullOrEmpty(conversationId) && !string.IsNullOrEmpty(channelId))
        {
            message.ChannelId = channelId;
        }
        else
        {
            conversationId = (await connector.Conversations.CreateDirectConversationAsync(botAccount, userAccount)).Id;
        }
        message.From = botAccount;
        message.Recipient = userAccount;
        message.Conversation = new ConversationAccount(id: conversationId);
        message.Text = msg;
        message.Locale = "en-Us";
        await connector.Conversations.SendToConversationAsync((Activity)message);
        return conversationId;
    }

}

以上是关于让bot在特定时间每天发送消息的主要内容,如果未能解决你的问题,请参考以下文章

Discord Bot 自动发送消息

当有人加入特定频道时发送消息 - Discord Bot

Discord BOT python,如何在特定用户编写命令时发送消息

Microsoft bot 框架获取用户时区

如何使用 discord.js 从 discord bot 向特定用户发送消息

如何每天在特定时间发送消息?