I am building a ChatBot using C# and Visual Studio. What I want to do is after some queries to the ChatBot, the user can't use the keyboard anymore, I've seen some websites that explain how to do it but I didn't understand well, so if someone can help me.
The idea is to put in the else block something that prevent the user to write to the ChatBot, so if he want to write the things that he type won't appear on the screen. Here is my code :
private int NombreDeMessages;
protected override async Task MessageReceived(IDialogContext context, IAwaitable<IMessageActivity> item)
{
var message = await item;
NombreDeMessages += 1;
string code = EndOfConversationCodes.CompletedSuccessfully;
CancellationToken cancellationToken = default(CancellationToken);
if (message.Text != null && NombreDeMessages < 3)
{
await base.MessageReceived(context, item);
}
else if (message.Text != null && NombreDeMessages == 3)
{
AdaptiveCard card = new AdaptiveCard();
card.Body.Add(new TextBlock()
{
Text = "STOP FLOODING",
Weight = TextWeight.Bolder,
IsSubtle = true,
Wrap = true,
Size = TextSize.Large
});
card.Body.Add(new TextBlock()
{
Text = "You have reach the limit of queries",
IsSubtle = false,
Wrap = true,
Size = TextSize.Normal
});
card.Body.Add(new Image()
{
Url = "http://images.roadtrafficsigns.com/img/dp/lg/traffic-stop-sign.png",
HorizontalAlignment = AdaptiveCards.HorizontalAlignment.Center,
Size = ImageSize.Stretch
});
Attachment attachment = new Attachment()
{
ContentType = AdaptiveCard.ContentType,
Content = card
};
var flood = context.MakeMessage();
flood.Attachments.Add(attachment);
await context.PostAsync(flood);
}
else
{
var reply = context.MakeMessage();
reply.Type = ActivityTypes.EndOfConversation;
reply.AsEndOfConversationActivity().Code = code;
await context.PostAsync(reply, cancellationToken);
}
}
Yes I want to block input on the web application, I don't have a C# application. My HTML code is this one:
<!DOCTYPE html>
<html>
<body style="width:100%;height:100%;">
<script>
(function () {
var div = document.createElement("div");
document.getElementsByTagName('body')[0].appendChild(div);
div.outerHTML = "<div id='botDiv' style='height: 38px; position: fixed; bottom: 0; z-index: 1000; background-color: #fff'><div id='botTitleBar' style='height: 38px; width: 400px; position:fixed; cursor: pointer;'></div><iframe width='400' height='600' src='https://webchat.botframework.com/embed/supportbotv2?s=4mHD9MekOvU.cwA.8ZU.Pn31pCpv8kyq7UQCr3y4bM-99W4eHxUt-LlhBnCp9JY'></iframe></div>";
document.querySelector('body').addEventListener('click', function (e) {
e.target.matches = e.target.matches || e.target.msMatchesSelector;
if (e.target.matches('#botTitleBar')) {
var botDiv = document.querySelector('#botDiv');
botDiv.style.height = botDiv.style.height == '600px' ? '38px' : '600px';
};
});
}());</script>
</body>
</html>
What's wrong with disabling the control where keyboard input is taken? For example:
this.textBox1.enabled = false
Related
We use PuppeteerSharp to add the ability for users to download a PDF of our application. Unfortunately sometimes a huge number of Chromium processes are started and don't stop until a server restart.
Normally when a user downloads a pdf, 5 chromium processes start and those disappear when the download is finished.
This is our code:
private static LaunchOptions launchOptions = new LaunchOptions
{
ExecutablePath = chromepath,
Headless = true,
DefaultViewport = ViewPortOptions
};
public static void ExportPdf(string url, string location)
{
try
{
Task<string> task = Task.Run<string>(async () => await ExportPdfASync(url, location)));
task.Wait();
}
catch (Exception)
{
// Exception handling
}
}
public static async Task<string> ExportPdfASync(string url, string location)
{
using (var browser = await Puppeteer.LaunchAsync(LaunchOptions))
using (var page = await browser.NewPageAsync())
{
await page.SetViewportAsync(new ViewPortOptions() { Width = 1440, Height = 990, IsMobile = false, DeviceScaleFactor = 1.0 });
await page.SetJavaScriptEnabledAsync(true);
await page.GoToAsync(url);
await page.WaitForTimeoutAsync(1500);
var marginOptions = new MarginOptions()
{
Top = "10mm",
Left = "10mm",
Right = "10mm",
Bottom = "10mm"
};
var pdfOptions = new PdfOptions()
{
PrintBackground = true,
Format = PaperFormat.A4,
MarginOptions = marginOptions,
Landscape = landscape
};
await page.PdfAsync(location, pdfOptions);
}
return "";
}
The browser and page are in the using blocks so, even with an error those should be disposed.
Does anyone have a solution for this problem? We, and our servers would be very happy;)
Try this:
public static async Task<string> ExportPdfASync(string url, string location)
{
try
{
using (var browser = await Puppeteer.LaunchAsync(LaunchOptions))
using (var page = await browser.NewPageAsync())
{
await page.SetViewportAsync(new ViewPortOptions() { Width = 1440, Height = 990, IsMobile = false, DeviceScaleFactor = 1.0 });
await page.SetJavaScriptEnabledAsync(true);
await page.GoToAsync(url);
await page.WaitForTimeoutAsync(1500);
var marginOptions = new MarginOptions()
{
Top = "10mm",
Left = "10mm",
Right = "10mm",
Bottom = "10mm"
};
var pdfOptions = new PdfOptions()
{
PrintBackground = true,
Format = PaperFormat.A4,
MarginOptions = marginOptions,
Landscape = landscape
};
await page.PdfAsync(location, pdfOptions);
}
}
catch (Exception ex)
{
}
finally {
browser.CloseAsync();
page.CloseAsync();
}
return "";
}
I encountered a “date” issue with Bot in TEAMS, The date value cannot render at LeaveRequest() properly. However, the same bot works fine in Bot Emulator.
Following is my sample code. How to fix (or workaround) this issue?
Select a date from date picker and submit
System does not render the first field value.
public class EchoBot
{
public static Attachment LeaveRequest()
{
AdaptiveSchemaVersion schemaVersion = new AdaptiveSchemaVersion(1, 0);
AdaptiveCard card = new AdaptiveCard(schemaVersion);
card.Body.Add(new AdaptiveTextBlock()
{
Text = "Enter new Date",
Size = AdaptiveTextSize.Large,
Weight = AdaptiveTextWeight.Bolder
});
AdaptiveDateInput fromDate = new AdaptiveDateInput()
{
Id = "FromDate",
Placeholder = "From Date"
};
card.Body.Add(fromDate);
AdaptiveTextInput toDate = new AdaptiveTextInput()
{
Id = "ToDate",
Placeholder = "To Date",
Value = DateTime.Today.ToUniversalTime().ToString("u")
};
card.Body.Add(toDate);
card.Actions.Add(new AdaptiveSubmitAction()
{
Title = "Submit"
});
Attachment cardAttachment = new Attachment()
{
ContentType = AdaptiveCard.ContentType,
Content = card
};
return cardAttachment;
}
}
public class RootDialog : IDialog<object>
{
public async Task StartAsync(IDialogContext context)
{
context.Wait(MessageReceivedAsync);
}
public virtual async Task MessageReceivedAsync(IDialogContext context,
IAwaitable<IMessageActivity> result)
{
var activity = await result as Activity;
Boolean first = true;
if (activity.Value != null)
{
var jObjectValue = activity.Value as JObject;
var fromDateString = jObjectValue.Value<string>("FromDate");
var toDateString = jObjectValue.Value<string>("ToDate");
await context.PostAsync($"from Date Entered: {fromDateString}");
await context.PostAsync($"to Date Entered: {toDateString}");
first = false;
}
if (first)
{
var reply = activity.CreateReply();
var card = EchoBot.LeaveRequest();
var msg = context.MakeMessage();
msg.Attachments.Add(card);
await context.PostAsync(msg);
}
}
}
Thanks for reaching out!! This is a known issue. We have a bug on this and we are working on getting this fixed.
Please help me out by sharing the step by step procedure to achieve the scanning functionality using Twain in ASP.Net MVC5. Thank you
Solution is here:
In ASP.Net/Core Project you send message to call winform project:
var start = function () {
var i = 0;
var wsImpl = window.WebSocket || window.MozWebSocket;
window.ws = new wsImpl('ws://localhost:8181/');
ws.onmessage = function (e) {
$('#submit').hide();
$('#scanBtn').hide();
$('.loader').show();
if (typeof e.data === "string") {
//IF Received Data is String
}
else if (e.data instanceof ArrayBuffer) {
//IF Received Data is ArrayBuffer
}
else if (e.data instanceof Blob) {
i++;
var f = e.data;
f.name = "File" + i;
storedFiles.push(f);
formdata.append(f.name, f);
var reader = new FileReader();
reader.onload = function (e) {
var html = "<div class=\"col-sm-2 text-center\"
style=\"border: 1px solid black; margin-left: 2px;\"><img
height=\"200px\" width=\"200px\" src=\"" + e.target.result + "\"
data-file='" + f.name + "' class='selFile' title='Click to
remove'><br/>" + i + "</div>";
selDiv.append(html);
$('#submit').show();
$('#scanBtn').show();
$('.loader').hide();
}
reader.readAsDataURL(f);
}
};
ws.onopen = function () {
//Do whatever u want when connected succesfully
};
ws.onclose = function () {
$('.dalert').modal('show');
};
}
window.onload = start;
function scanImage() {
ws.send("1100");
};
https://javascript.info/websocket
In Winforms Project you scan document and send graphic data back to Asp.Net/Core project:
public partial class Form1 : Form
{
ImageCodecInfo _tiffCodecInfo;
TwainSession _twain;
bool _stopScan;
bool _loadingCaps;
List allSockets;
WebSocketServer server;
public Form1()
{
InitializeComponent();
if (NTwain.PlatformInfo.Current.IsApp64Bit)
{
Text = Text + " (64bit)";
}
else
{
Text = Text + " (32bit)";
}
foreach (var enc in ImageCodecInfo.GetImageEncoders())
{
if (enc.MimeType == "image/tiff") { _tiffCodecInfo = enc; break; }
}
this.WindowState = FormWindowState.Minimized;
this.ShowInTaskbar = false;
allSockets = new List<IWebSocketConnection>();
server = new WebSocketServer("ws://0.0.0.0:8181");
server.Start(socket =>
{
socket.OnOpen = () =>
{
Console.WriteLine("Open!");
allSockets.Add(socket);
};
socket.OnClose = () =>
{
Console.WriteLine("Close!");
allSockets.Remove(socket);
};
socket.OnMessage = message =>
{
if (message == "1100")
{
this.Invoke(new Action(()=> {
this.WindowState = FormWindowState.Normal;
}));
}
};
});
}
Link to project.
https://github.com/mgriit/ScanAppForWeb
You can remake this project, as you want.
At this moment, none of the browsers support scanning out of the box. You need to use a third-party library (not part of Microsoft's .NET core components). Below example uses Scanner.js, which is a product offered by our company:
Enable Scanning from TWAIN Scanners to ASP.NET Pages: Step by Step
Below steps use Scanner.js as example; they may differ for other products.
1) Include the scanning library in your HTML code:
<script type="text/javascript" src="//asprise.azureedge.net/scannerjs/scanner.js"></script>
2) Add a button to trigger the scanning process:
function scanToJpg() {
scanner.scan(displayImagesOnPage,
{
"twain_cap_setting" : {
"ICAP_PIXELTYPE" : "TWPT_RGB", // Color
"ICAP_XRESOLUTION" : "100", // DPI: 100
"ICAP_YRESOLUTION" : "100",
"ICAP_SUPPORTEDSIZES" : "TWSS_USLETTER" // Paper size: TWSS_USLETTER, TWSS_A4, ...
},
"output_settings" :
[
{
"type" : "return-base64",
"format" : "jpg"
}
]
}
);
}
3) Handle the scan result - display, upload, etc.
Below code creates an img element for each image scanned to display on the current web page:
/** Processes the scan result */
function displayImagesOnPage(successful, mesg, response) {
var scannedImages = scanner.getScannedImage(response, true, false); // returns an array of ScannedImage
for(var i = 0; (scannedImages instanceof Array) && i < scannedImages.length; i++) {
var scannedImage = scannedImages[i];
processScannedImage(scannedImage);
}
}
/** Images scanned so far. */
var imagesScanned = [];
/** Processes a ScannedImage */
function processScannedImage(scannedImage) {
imagesScanned.push(scannedImage);
var elementImg = createDomElementFromModel( {
'name': 'img',
'attributes': {
'class': 'scanned',
'src': scannedImage.src
}
});
document.getElementById('images').appendChild(elementImg);
}
For examples of scanning into PDF formats and direct uploading, please visit the code repository: https://github.com/Asprise/scannerjs.javascript-scanner-access-in-browsers-chrome-ie.scanner.js
I am submitting a form in javascript to a controller in C# MVC it submits easily in chrome but not in Firefox and IE
//CSHTML CODE
<th class="gen2">
<button type="button" id="buttonClass">Generate</button>
</th>
<td class="money"><input type="checkbox" class="chk" name="checkboxID" value=#item.WithdrawalID></td>
//Javascript code
$("#buttonClass").click(function () {
getValueUsingClass();
});
function getValueUsingClass() {
var data = "";
var submitForm = document.createElement('form');
//Creating a form and giving the attributes
submitForm.name = "formSubmit";
submitForm.id = "formSubmit";
submitForm.method = "post";
submitForm.action = "generatebankfile";
var chkArray = \[\];
alert(chkArray);
$(".chk:checked").each(function () {
chkArray.push($(this).val());
});
for (var i = 0; i < chkArray.length; i++) {
data = data + chkArray\[i\];
if (i != chkArray.length - 1) {
data = data + ',';
}
}
var element = document.createElement("input");
element.name = "checkboxID";
element.value = data;
submitForm.appendChild(element);
if (chkArray.length > 0) {
submitForm.submit();
}
else {
alert("Please select at least one of the checkbox");
}
}
append form to the body
document.getElementsByTagName('body')[0].appendChild(submitForm);
Hi I am trying to get an "UpDown" button which allows the user to hold the increment/decrement button to quickly and easily increment/decrement a decimal value. I have been trying this using the ajaxToolkit:NumericUpDownExtender but this seems to only allow increment/decrement with button clicks. This is rather clunky since the value is a percentage. Any ideas of a better way to handle this within ASP.NET?
I was able to make this pretty simply with javascript using a timer.
Here is the ASPX part of it:
<asp:TextBox ID="Factor" runat="server" MaxLength="6" Width="60"/>
<input id ="UpButton" value="▲" type="button" onmousedown="timerID = setInterval(function(){FactUp()},100);" onmouseup="clearInterval(timerID);"/>
<input id ="DownButton" value="▼" type="button" onmousedown="timerID = setInterval(function(){FactDown()},100);" onmouseup="clearInterval(timerID);"/>
And the javascript:
var timerID = 0;
function FactDown() {
var obj = document.getElementById('Factor');
var num = parseFloat(obj.value)
if (isNaN(num)) {
return;
}
num -=0.01;
obj.value = num.toFixed(2);
}
function FactUp() {
var obj = document.getElementById('Factor');
var num = parseFloat(obj.value);
if (isNaN(num)) {
return;
}
num += 0.01;
obj.value = num.toFixed(2);
}
Add reference on this script to ScriptManager control's Script collection:
Sys.Extended.UI.NumericUpDownBehavior.prototype._clearClickInterval = function () {
if (this._clickInterval != null) {
window.clearInterval(this._clickInterval);
this._clickInterval = null;
}
};
Sys.Extended.UI.NumericUpDownBehavior.prototype._setDownClickInterval = function () {
this._clearClickInterval();
this._clickInterval = window.setInterval(this._clickDownHandler, 200);
};
Sys.Extended.UI.NumericUpDownBehavior.prototype._setUpClickInterval = function () {
this._clearClickInterval();
this._clickInterval = window.setInterval(this._clickUpHandler, 200);
};
Sys.Extended.UI.NumericUpDownBehavior.prototype.addIntervalHandlers = function () {
this._clickInterval = null;
this._buttonMouseUpHandler = Function.createDelegate(this, this._clearClickInterval);
if (this._bUp) {
this._upButtonMouseDownHandler = Function.createDelegate(this, this._setUpClickInterval);
$addHandler(this._bUp, 'mousedown', this._upButtonMouseDownHandler);
$addHandler(window, 'mouseup', this._buttonMouseUpHandler);
}
if (this._bDown) {
this._downButtonMouseDownHandler = Function.createDelegate(this, this._setDownClickInterval);
$addHandler(this._bDown, 'mousedown', this._downButtonMouseDownHandler);
$addHandler(window, 'mouseup', this._buttonMouseUpHandler);
}
};
var legacyInitialize = Sys.Extended.UI.NumericUpDownBehavior.prototype.initialize,
legacyDispose = Sys.Extended.UI.NumericUpDownBehavior.prototype.dispose;
Sys.Extended.UI.NumericUpDownBehavior.prototype.initialize = function () {
legacyInitialize.apply(this);
this.addIntervalHandlers();
};
Sys.Extended.UI.NumericUpDownBehavior.prototype.dispose = function () {
legacyDispose.apply(this);
this._clearClickInterval();
if (this._upButtonMouseDownHandler) {
$removeHandler(this._bUp, 'mousedown', this._upButtonMouseDownHandler);
$removeHandler(window, 'mouseup', this._buttonMouseUpHandler);
this._upButtonMouseDownHandler = null;
}
if (this._downButtonMouseDownHandler) {
$removeHandler(this._bDown, 'mousedown', this._downButtonMouseDownHandler);
$removeHandler(window, 'mouseup', this._buttonMouseUpHandler);
this._downButtonMouseDownHandler = null;
}
this._buttonMouseUpHandler = null;
};
Not sure, but might be required to call addIntervalHandlers() function expicitely on an extender instance.
You can check this script if you'll add at the end this line: $find("ctl00_SampleContent_NumericUpDownExtender4").addIntervalHandlers() and execute it from browser's console on this page: NumericUpDown Demonstration The last extender will handle mouse button hold.