C# 让机器人每天在特定时间发送消息
我正在使用C# 让机器人每天在特定时间发送消息,c#,.net,botframework,bots,C#,.net,Botframework,Bots,我正在使用bot框架构建一个bot,它应该在MS团队中运行,我希望它每天早上6:30给我发送一条消息 我有一个每天6:30在程序文件中调用的方法。 我有一个从机器人发送消息的方法 这是我的计时器的代码: private static Timer _timer; private static int count = 1; public static void Main(string[] args) { //Initialization
bot框架构建一个bot
,它应该在MS团队中运行
,我希望它每天早上6:30给我发送一条消息
我有一个每天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));
}
但我找不到一个方法来存档它。。。非常感谢您的帮助,我已经搜索了很多地方,但没有发现类似的问题
问题是所有的名称空间(例如iTunesContext、Conversationstate等…)
希望这能描述我的问题
提前谢谢
编辑:
它不一定需要是AlertSubscribers()
方法,而是一个函数,它只是一个做类似事情的代码
我已尝试了此代码,但无法使bot向用户发送消息(在本例中,我在Emulator中):
我是机器人框架的新手,所以我的代码和想法可能是错误的
解决了它强>
public static async void callTimerMethod()
{
await ConversationStarter.Resume("conversationId", "emulator");
}
我必须将callTimerMethod()
更改为异步方法,并创建一个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;
}
}
公共类会话启动程序
{
公共静态字符串fromId;
公共静态字符串fromName;
公共静态字符串toId;
公共静态字符串toName;
公共静态字符串serviceUrl;
公共静态字符串channelId;
公共静态字符串会话ID;
公共静态异步任务恢复(字符串会话ID、字符串通道ID)
{
conversationId=等待通话(conversationId,channelId,$“你好!”);
conversationId=等待通话(conversationId,channelId,$“这是通知!”);
}
专用静态异步任务对话(字符串对话ID、字符串通道ID、字符串消息)
{
var userAccount=新的ChannelAccount(toId,toName);
var botAccount=新的ChannelAccount(fromId,fromName);
var connector=newconnectorclient(新Uri(serviceUrl));
IMessageActivity message=Activity.CreateMessageActivity();
如果(!string.IsNullOrEmpty(conversationId)和&!string.IsNullOrEmpty(channelId))
{
message.ChannelId=ChannelId;
}
其他的
{
conversationId=(wait connector.Conversations.CreateDirectConversationAsync(botAccount,userAccount)).Id;
}
message.From=botAccount;
message.Recipient=userAccount;
message.Conversation=新的ConversationAccount(id:conversationId);
message.Text=msg;
message.Locale=“en-Us”;
等待connector.Conversations.SendToConversationAsync((活动)消息);
返回会话ID;
}
}
public static async void callTimerMethod()
{
await ConversationStarter.Resume("conversationId", "emulator");
}
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;
}
}