Microsoft Bot Framework Special Characters - c#

A coworker of mine developed a bot using MS Bot Framework. It's working as expected, but there are responses wherein special characters are shown in place of apostrophe's. Please let me know if you guys experienced this and have any fix. Thanks. :)

You can use regex class for a best practice to avoid other special characters in future. Below is the sample you can use
using System;
using System.Text.RegularExpressions;
class Program
{
static void Main()
{
string inputString = "Manoj# Bhard#waj";
// . indicates any character to be removed. You can also write characters as well to remove some selected characters
string outputString = Regex.Replace(input, ".", " ");
// Write the output.
Console.WriteLine(inputString);
Console.WriteLine(outputString);
}
}

I also faced this issue, then I created the message with below formats in MS bot Code(app.js).
var customMessage = new builder.Message(session)
.text("I didn't quite get that. For us ......")
.textFormat("plain")
.textLocale("en-us");
session.send(customMessage);
Refer Official URL for V3: https://learn.microsoft.com/en-us/azure/bot-service/nodejs/bot-builder-nodejs-message-create?view=azure-bot-service-3.0

Related

I need to strip a Google Alerts URL

To preface, I know there are similar threads about this, but I am using C#, not java, or python, or Php. Some threads provide a solution for a single URL, which is not universal. Thanks for not flagging me.
So I am using Google Alerts to get links to articles via email. I have already written a program that can strip the URLs out of the email as well as another program to scrape the websites. My issue is that the links in the google alerts email look like this:
https://www.google.com/url?rct=j&sa=t&url=http://www.foxnews.com/health/2016/08/19/virtual-reality-treadmills-help-prevent-falls-in-elderly.html&ct=ga&cd=CAEYACoTOTc2NjE4NjYyNzMzNzc3NDcyODIaODk2NWUwYzRjMzdmOGI4Nzpjb206ZW46VVM&usg=AFQjCNGyK2EyVBLoKnNkdxIBDf8a_B3Ung. Yeah, ugly.
Because this redirects to the actual article through google, my scraping program does not work on these links. I have tried a million different RegExs from questions here and other sources. I managed to strip off everything up until the http:// of the actual article but it still has the tail end that screws it up. Here is what I have so far. They now look like:
http://www.foxnews.com/health/2016/08/19/virtual-reality-treadmills-help-prevent-falls-in-elderly.html&ct=ga&cd=CAEYACoTOTc2NjE4NjYyNzMzNzc3NDcyODIaODk2NWUwYzRjMzdmOGI4Nzpjb206ZW46VVM&usg=AFQjCNGyK2EyVBLoKnNkdxIBDf8a_B3Ung
private List<string> GetLinks(string message)
{
List<string> list = new List<string>();
Regex urlRx = new Regex(#"((http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,#?^=%&:/~\+#]*[\w\-\#?^=%&/~\+#])?)", RegexOptions.IgnoreCase);
MatchCollection matches = urlRx.Matches(message);
foreach (Match match in matches)
{
if(!match.ToString().Contains("news.google.com/news") && !match.ToString().Contains("google.com/alerts"))
{
string find = "=http";
int ind = match.ToString().IndexOf(find);
list.Add(match.ToString().Substring(ind+1));
}
}
return list;
}
Some help getting rid of the endings would be awesome, be it a new RegEx or some extra code. Thanks in advance.
You can use HttpUtility.ParseQueryString to retrieve the url part of the query string. It is located in the System.Web namespace (reference required).
var uri = new Uri("https://www.google.com/url?rct=j&sa=t&url=http://www.foxnews.com/health/2016/08/19/virtual-reality-treadmills-help-prevent-falls-in-elderly.html&ct=ga&cd=CAEYACoTOTc2NjE4NjYyNzMzNzc3NDcyODIaODk2NWUwYzRjMzdmOGI4Nzpjb206ZW46VVM&usg=AFQjCNGyK2EyVBLoKnNkdxIBDf8a_B3Ung");
var queries = HttpUtility.ParseQueryString(uri.Query);
var foxNews = queries["url"]; //http://www.foxnews.com/health/2016/08/19/virtual-reality-treadmills-help-prevent-falls-in-elderly.html

Encoding a line break for Twilio SMS using C#?

I'm using WebApi 2.2 to ramp up on the Twilio API. I have the Twilio C# libraries installed.
I'm using a form to capture a string in my web app, and I send that down to webAPI. I'm wondering how I can leverage the C# libraries to send a message with line breaks.
The example code shows the following:
var msg = twilio.SendMessage("+15084043345", "+15084043345", "Can you believe it's this easy to send an SMS?!");
However, I'm not sure how to include a line break in this message. Should I be inserting anything into this string clientside to represent a linebreak? Any guidance would be awesome.
Twilio Evangelist here.
This is actually super simple:
var msg = twilio.SendMessage("+15084043345", "+15084043345", "Hello.\nCan you believe it's this easy to send an SMS?!");
Will result in:
Hello.
Can you believe it's this easy to send an SMS?!
Just like printing strings to the console, \n, \r\n, and \r can be used to insert a new line. I can't say definitively what is best to use across all handsets, but I have found \n to be pretty reliable. (I can say all three of these work on my iOS 8 device perfectly...)
If you want to show new line in sms like
Account Name : suraj
Age : 24
then here is a code for asp.net VB
Dim value As String = "Account Name :" & "suraj" & vbCrLf & "Age :" & " 24"
it will show new line in SMS not in web page

Regex Issue in C#

I am trying to create a C# routine that removes all of the following prefixes and suffixes and returns just the root word of a domain:
var stripChars = new List<string> { "http://", "https://", "www.", "ftp.", ".com", ".net", ".org", ".info", ".co", ".me", ".mobi", ".us", ".biz" };
I do this with the following code:
originalDomain = stripChars.Aggregate(originalDomain, (current, repl) => Regex.Replace(current, repl, #"", RegexOptions.IgnoreCase));
Which seems to work in almost all cases. Today, however, I discovered that setting "originalDomain" to "NameCheap.com" does not return:
NameCheap
Like it should, but rather:
NCheap
Can anyone look at this and tell me what is going wrong? Any help would be appreciated.
THis is normal: the dot in a regex means any character.
Therefore, .me matches ame in NameCheap.
Escape the dots with a backslash.
Also, you'd be better off using a dedicated URI API for this kind of operation.
I know this doesn't answer your question directly, but given the specific task you are trying to accomplish I would recommend trying something like this:
Uri uri = new Uri(originalDomain);
originalDomain = uri.Host;
EDIT:
If your input may not contain a scheme you can use the uri builder as notied in this post
var hostName = new UriBuilder(input).Host
Hope this helps.

Strange XML modification when input and output it in console app using Visual Studio

I have a simple console app that was creted in c# using VisualStudio. It has a input parameter and return it to output.
This is the code:
static void Main(string[] args)
{
string msg = args[0];
Console.WriteLine(msg);
}
Then I try to pass a param that contain a xml as string: "<messages><message>message</message></messages>". There is nothing wrong if I will use console for calling application. But in the case when I trying to debug such application I have added parameter string to Command line arguments on the Debug tab.
After this I have got unexpected output like:
^<messages^>^<message^>message^</message^>^</messages^>
Why we have such output and how to overcome this? Thanks in advance.
The simplest solution for this issue should be replacing unwilling characters like here:
static void Main(string[] args)
{
string msg = args[0];
Console.WriteLine(msg.Replace("^","");
}
But the reason of such behavior placed somewhere in the core of the operation system. Windows handle this situation like launch a batch file from command line with parameters. Following the link that was presented in the answer of #Artem K. you can find out that here is two way to overcome such situation. Unfortunately, no one work with this issue. Possible, because Visual Studio add something from yourself in the question how to pass args for launching batch.
How to avoid cmd.exe interpreting shell special characters like < > ^
in Shell, the < and > has special meanings concidering steam redirection..
what makes dir /w > myDirectory.txt possible or tree | more Etc...
Follow #Artem Koshelev post to get around that.

Please help me to convert this java code to C#

I'm using Htmlunit in my C# project, but I cannot convert this code in java to C#
webClient.setWebConnection(new HttpWebConnection(webClient) {
public WebResponse getResponse(WebRequestSettings settings) throws IOException {
System.out.println(settings.getUrl());
return super.getResponse(settings);
}
});
Can anyone kindly convert it to C# ?
Thank in advance
I don't think that this might be the right conversion, but you can try it and let us know, as "HtmlUnit is a class library for Java that lets you programmatically play with web sites."
One think to remember that although the syntax between Java and C# looks the same, the way exceptions are declared is treated differently. In Java your class methods needs to identify the exceptions that it could possibly throw, where as this is not the case in C#, from there my feeble attempt to rewrite the piece of code.
Another thing I've picked up is the defining of methods within the object creation, but that might be part of the HtmlUnit framework.
webClient.setWebConnection(new HttpWebConnection(webClient) {
public WebResponse getResponse(WebRequestSettings settings) {
System.out.println(settings.getUrl());
return this.getResponse(settings);
}
});
If it is not working, it might just nudge you in the right direction.
You might be looking for the .NET WebClient class and the code examples under its Download*(..) methods. e.g.
using System.Net;
One example of many, copied from MSDN:
Console.Write("\nPlease enter a URI (for example, http://www.contoso.com): ");
string remoteUri = Console.ReadLine();
// Create a new WebClient instance.
WebClient myWebClient = new WebClient();
// Download home page data.
Console.WriteLine("Downloading " + remoteUri);
// Download the Web resource and save it into a data buffer.
byte[] myDataBuffer = myWebClient.DownloadData (remoteUri);
// Display the downloaded data.
string download = Encoding.ASCII.GetString(myDataBuffer);
Console.WriteLine(download);
Console.WriteLine("Download successful.");

Categories