我有一个这样的场景:需要一个团队对话机器人在每天的特定时间向群聊发送主动消息。
我的方法是:在函数中设置一个时间触发器,当它触发时,我的函数会调用一个我自己创建的API,这个API会执行发送主动消息的方法。
这是我的功能代码,希望对您有所帮助:
#r "Newtonsoft.Json"
using System.Net;
using System.IO;
using System.IO.Compression;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;
public static void Run(TimerInfo myTimer, ILogger log)
{
log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://xxxxxxxx.azurewebsites.net/api/sendProactiveMesg");
request.Method = "GET";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream myResponseStream = response.GetResponseStream();
}
============更新============
根据您的错误信息,您似乎在某些地方错过了配置,但我不知道为什么会这样。我将分享我在创建时间触发器方面的经验。保存代码后,每分钟触发一次。如果这没有帮助,你能描述一下你的配置吗?因为我从来没有遇到过'none value'的时间触发错误。
=========================更新2 ==================== =========
我的代码:
public async Task sendtoPersonal()
{
string teamInternalId = "19:3fxxx7d8c@unq.gbl.spaces";
string serviceUrl = "https://smba.trafficmanager.net/amer/";
string mentionUserPrincipalName = "xxx";
string tenantId = "72fxxxx47";
string botClientID = "e8caxxxx8c";
string botClientSecret = "5zxxxxJA";
AppCredentials.TrustServiceUrl(serviceUrl);
ConnectorClient connectorClient = new ConnectorClient(new Uri(serviceUrl), new MicrosoftAppCredentials(botClientID, botClientSecret), true);
TeamsChannelAccount user2 = (await connectorClient.Conversations.GetConversationMembersAsync(teamInternalId)).Select((ChannelAccount channelAccount) => JObject.FromObject(channelAccount).ToObject<TeamsChannelAccount>()).First((TeamsChannelAccount user) => user.UserPrincipalName == mentionUserPrincipalName);
//Activity personalMessageActivity = MessageFactory.Text("Personal message from the Bot!");
IMessageActivity personalMessageActivity = await showTeamStatus();
ConversationParameters conversationParameters = new ConversationParameters
{
Members = new List<ChannelAccount>
{
user2
},
ChannelData = new TeamsChannelData
{
Tenant = new TenantInfo
{
Id = tenantId
}
}
};
ConversationResourceResponse response = await connectorClient.Conversations.CreateConversationAsync(conversationParameters);
await connectorClient.Conversations.SendToConversationAsync(response.Id, (Activity)personalMessageActivity);
}