我已经使用Bot Framework v4 c#在本地创建了一个Bot。它有一个欢迎卡,当我将本地URL与模拟器连接后,该卡会自动弹出,但是最近我将我的机器人部署在Azure上,并使用直接通道将其集成到我的网站中。现在,每当我单击时,它都会打开该机器人,但是欢迎卡并不会自动出现,当我从聊天机器人中写一些东西时,它就会出现。我只希望欢迎卡自动出现在模拟器中。你们可以帮我吗?以下是我正在网站中集成的直线代码。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<!-- Paste line 7 to 27 after the title tag in _Layout.cshtml -->
<link href="https://cdn.botframework.com/botframework-webchat/latest/botchat.css" rel="stylesheet"
/>
<script src="https://cdn.botframework.com/botframework-webchat/latest/botchat.js"></script>
<style>
#mychat {
margin: 10px;
position: fixed;
bottom: 30px;
left: 10px;
z-index: 1000000;
}
.botIcon {
float: left !important;
border-radius: 50%;
}
.userIcon {
float: right !important;
border-radius: 50%;
}
</style>
</head>
< body>
<!-- Paste line from 31 to 33 before the </body> tag at the end of code -->
<div id="container">
<img id="mychat" src=""/>
</div>
</body>
<!-- Paste line 38 to 88 after the </html> tag -->
<script>
(function () {
var div = document.createElement("div");
var user = {
id: "",name: ''
};
var bot = {
id: '',name: 'SaathiBot'
};
const botConnection = new BotChat.DirectLine({
secret: '',webSocket: false
})
document.getElementsByTagName('body')[0].appendChild(div);
div.outerhtml = "<div id='botDiv' style='width: 400px; height: 0px; margin:10px; position:
fixed; bottom: 0; left:0; z-index: 1000;><div id='botTitleBar' style='height: 40px; width: 400px;
position:fixed;光标:指针;'>“;
BotChat.App({
botConnection: botConnection,user: user,bot: bot
},document.getElementById("botDiv"));
document.getElementsByClassname("wc-header")[0].setattribute("id","chatbotheader");
document.querySelector('body').addEventListener('click',function (e) {
e.target.matches = e.target.matches || e.target.msMatchesSelector;
if (e.target.matches('#chatbotheader')) {
var botDiv = document.querySelector('#botDiv');
botDiv.style.height = "0px";
document.getElementById("mychat").style.display = "block";
};
});
document.getElementById("mychat").addEventListener("click",function (e) {
document.getElementById("botDiv").style.height = '500px';
e.target.style.display = "none";
})
}());
</script>
这也是我在C#中的欢迎卡代码
namespace microsoft.BotBuilderSamples
{
public class WelcomeUser : SaathiDialogBot<MainDialog>
{
protected readonly string[] _cards =
{
Path.Combine(".","Resources","WelcomeCard.json"),};
public WelcomeUser(ConversationState conversationState,UserState userState,MainDialog dialog,ILogger<SaathiDialogBot<MainDialog>> logger)
: base(conversationState,userState,dialog,logger)
{
}
protected override async Task OnmembersAddedAsync(IList<Channelaccount> membersAdded,ITurnContext<IConversationUpdateactivity> turnContext,CancellationToken cancellationToken)
{
await SendWelcomeMessageAsync(turnContext,cancellationToken);
Random r = new Random();
var cardAttachment = CreateAdaptiveCardAttachment(_cards[r.Next(_cards.Length)]);
await turnContext.SendactivityAsync(MessageFactory.Attachment(cardAttachment),cancellationToken);
}
private static async Task SendWelcomeMessageAsync(ITurnContext turnContext,CancellationToken cancellationToken)
{
foreach (var member in turnContext.activity.MembersAdded)
{
if (member.Id != turnContext.activity.Recipient.Id)
{
if (DateTime.Now.Hour < 12)
{
await turnContext.SendactivityAsync(
$"Hi,Good Morning {member.Name}",cancellationToken: cancellationToken);
}
else if (DateTime.Now.Hour < 17)
{
await turnContext.SendactivityAsync(
$"Hi,Good Afternoon {member.Name}",cancellationToken: cancellationToken);
}
else
{
await turnContext.SendactivityAsync(
$"Hi,Good Evening {member.Name}",cancellationToken: cancellationToken);
}
}
}
}
private static Attachment CreateAdaptiveCardAttachment(string filePath)
{
var adaptiveCardJson = File.ReadAllText(filePath);
var adaptiveCardAttachment = new Attachment()
{
ContentType = "application/vnd.microsoft.card.adaptive",Content = JsonConvert.DeserializeObject(adaptiveCardJson),};
return adaptiveCardAttachment;
}
}
}
这是在欢迎卡中继承的saathiDialog代码。这是我的bot文件夹中的两个文件
public class SaathiDialogBot<T> : activityHandler where T : Dialog
{
protected readonly BotState ConversationState;
protected readonly Dialog Dialog;
protected readonly ILogger Logger;
protected readonly BotState UserState;
private DialogSet Dialogs { get; set; }
public SaathiDialogBot(ConversationState conversationState,T dialog,ILogger<SaathiDialogBot<T>> logger)
{
ConversationState = conversationState;
UserState = userState;
Dialog = dialog;
Logger = logger;
}
public override async Task OnTurnAsync(ITurnContext turnContext,CancellationToken cancellationToken = default(CancellationToken))
{
var activity = turnContext.activity;
if (string.IsnullOrWhiteSpace(activity.Text) && activity.Value != null)
{
activity.Text = JsonConvert.SerializeObject(activity.Value);
}
if (turnContext.activity.Text == "Yes")
{
await turnContext.SendactivityAsync($"Good bye. I will be here if you need me. ",cancellationToken: cancellationToken);
await turnContext.SendactivityAsync($"Say Hi to wake me up.",cancellationToken: cancellationToken);
}
else
{
await base.OnTurnAsync(turnContext,cancellationToken);
}
await ConversationState.SaveChangesAsync(turnContext,false,cancellationToken);
await UserState.SaveChangesAsync(turnContext,cancellationToken);
}
protected override async Task OnmessageactivityAsync(ITurnContext<IMessageactivity> turnContext,CancellationToken cancellationToken)
{
Logger.LogInformation("Running dialog with Message activity.");
await Dialog.RunAsync(turnContext,ConversationState.CreateProperty<DialogState>(nameof(DialogState)),cancellationToken);
}
}
}
here is main Dialog code
namespace microsoft.BotBuilderSamples { public class MainDialog : ComponentDialog { private const string UserInfo = "value-userInfo"; protected readonly ILogger _logger; protected readonly string[] _cards = { Path.Combine(".","ValidationCard.json"),}; public MainDialog(ILogger<MainDialog> logger) : base(nameof(MainDialog)) { _logger = logger; AddDialog(new ProductIssue($"{nameof(MainDialog)}.fromMain")); AddDialog(new ProductIssue($"{nameof(Confirm)}.fromConfirm")); AddDialog(new ProductIssue($"{ nameof(Resolution)}.resolution")); AddDialog(new Confirm()); AddDialog(new ChoicePrompt($"{nameof(MainDialog)}.issue")); AddDialog(new TextPrompt($"{nameof(MainDialog)}.callDialog")); AddDialog(new NumberPrompt<int>($"{nameof(MainDialog)}.num",valinatiotionAsync)); AddDialog(new WaterfallDialog($"{nameof(MainDialog)}.mainFlow",new WaterfallStep[]
{
MoblieNumberAsync,ChoiceCardStepAsync,ShowCardStepAsync,CallingDialogsAsync
}));
AddDialog(new TextPrompt(nameof(TextPrompt)));
InitialDialogId = $"{nameof(MainDialog)}.mainFlow";
}
private async Task<DialogTurnResult> MoblieNumberAsync(WaterfallStepContext stepContext,CancellationToken cancellationToken)
{
stepContext.Values[UserInfo] = new UserInput();
var options = new PromptOptions()
{
Prompt = MessageFactory.Text("Kindly enter your 10 digit mobile number without any spaces,dashes and country code. We will be sending an OTP later to this number "),RetryPrompt = MessageFactory.Text("Incorrect mobile number entered. Please only enter the 10 digits of your mobile without any spaces,dashes and country code.")
};
return await stepContext.PromptAsync($"{nameof(MainDialog)}.num",options,cancellationToken);
}
private async Task<DialogTurnResult> ChoiceCardStepAsync(WaterfallStepContext stepContext,CancellationToken cancellationToken)
{
BotAPIBLL botApiBLL = new BotAPIBLL();
var response = botApiBLL.getcustomerDetail(stepContext.Context.activity.Text);
await stepContext.Context.SendactivityAsync(MessageFactory.Text("Fetching your details from our systems. This may take a moment"),cancellationToken);
var options = new PromptOptions()
{
Prompt = MessageFactory.Text("Welcome user,How can we serve you ? "),RetryPrompt = MessageFactory.Text("That was not a valid choice,please select a option between 1 to 4."),Choices = getchoices(),};
return await stepContext.PromptAsync($"{nameof(MainDialog)}.issue",cancellationToken);
}
private async Task<DialogTurnResult> ShowCardStepAsync(WaterfallStepContext stepContext,CancellationToken cancellationToken)
{
var attachments = new List<Attachment>();
var reply = MessageFactory.Text("");
var user_choice = ((FoundChoice)stepContext.Result).Value;
switch (user_choice)
{
case "Product issue":
reply.Attachments.Add(Cards.CreateAdaptiveCardAttachment3());
break;
case "Register Product":
reply.Attachments.Add(Cards.GetHeroCard1().ToAttachment());
break;
case "Online Purchase":
reply.Attachments.Add(Cards.GetHeroCard2().ToAttachment());
break;
case "Customer Grivance":
reply.Attachments.Add(Cards.GetHeroCard3().ToAttachment());
break;
default:
reply.AttachmentLayout = AttachmentLayoutTypes.Carousel;
reply.Attachments.Add(Cards.CreateAdaptiveCardAttachment3());
reply.AttachmentLayout = AttachmentLayoutTypes.Carousel;
reply.Attachments.Add(Cards.GetHeroCard1().ToAttachment());
break;
}
if (user_choice == "Register Product" || user_choice == "Online Purchase" || user_choice == "Customer Grivance")
{
await stepContext.Context.SendactivityAsync(reply,cancellationToken);
Random r = new Random();
var validationcard = Cards.CreateAdaptiveCardAttachment2(_cards[r.Next(_cards.Length)]);
await stepContext.Context.SendactivityAsync(MessageFactory.Attachment(validationcard),cancellationToken);
return await stepContext.EndDialogAsync(null,cancellationToken);
}
else
{
var options2 = new PromptOptions() { Prompt = reply,RetryPrompt = MessageFactory.Text("Retry") };
return await stepContext.PromptAsync($"{nameof(MainDialog)}.callDialog",options2,cancellationToken);
}
}
private async Task<DialogTurnResult> CallingDialogsAsync(WaterfallStepContext stepContext,CancellationToken cancellationToken)
{
string choice = stepContext.Result.ToString();
if (choice.ToLower() == "no")
{
return await stepContext.BeginDialogAsync($"{nameof(MainDialog)}.fromMain",null,cancellationToken);
}
else
return await stepContext.BeginDialogAsync(nameof(Confirm),cancellationToken);
}
private IList<Choice> getchoices()
{
var cardOptions = new List<Choice>()
{
new Choice() { Value = "Product issue",Synonyms = new List<string>() { "adaptive" } },new Choice() { Value = "Register Product",Synonyms = new List<string>() { "hero" } },new Choice() { Value = "Online Purchase",new Choice() { Value = "Customer Grivance",};
return cardOptions;
}
private Task<bool> valinatiotionAsync(PromptValidatorContext<int> promptContext,CancellationToken cancellationToken)
{
string value = (string)promptContext.Context.activity.Text;
if (Regex.IsMatch(value,"^[0-9]{10}$"))
{
return Task.FromResult(true);
}
else
{
return Task.FromResult(false);
}
}
}
}