让漫游器每天在特定时间发送消息

时间:2019-03-06 10:23:07

标签: c# .net botframework bots

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

我有一个每天Program文件中每天6:30调用的方法。 而且我有一种方法可以从漫游器发送消息。

这是我的计时器的代码:

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()方法,但是函数och只能执行类似的功能。

我已经尝试过这段代码,但是我无法获得它来使机器人将消息发送给用户(在这种情况下,我是在模拟器中):

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框架的新手,所以我的代码和想法可能是错误的...

1 个答案:

答案 0 :(得分:0)

解决了!

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;
    }

}