I am using Microsoft Bot Framework and trying to send multiple responses to user but not sure how to do that from nodejs. I am able to do that from C#.
C# code example
var connector = new ConnectorClient();
connector.Messages.SendMessage(incomingMessage.CreateReplyMessage("Yo, I heard you 1.", "en"));
connector.Messages.SendMessage(incomingMessage.CreateReplyMessage("Yo, I heard you 2.", "en"));
connector.Messages.SendMessage(incomingMessage.CreateReplyMessage("Yo, I heard you 3.", "en"));
What would this be in nodejs?
nodejs code example that I'm trying to run:
var bot = new builder.TextBot();
bot.add('/', function (session) {
session.send('Hello World');
session.send('Hello World'); // won't work.
});
bot.listenStdin();
Checkout this answer: send multiple responses to client via nodejs
You may want simulate the multiple responses(solution 1) that you want by returning an array property with the response object. In this case it might be helpful using this lib: https://github.com/caolan/async if you are using multiple asynchronous functions.
var messages = [];
messages.push('message 1');
messages.push('message 2');
// etc
response.send({all: messages});
However if you are not able to so so I would recommend the solution 3 since it will keep your code cleaner & most complex web apps use websockets anyway for making the website to not require refreshed for any new data that are needed to be retrieved.
A commonly used library for websockets is socketio: http://socket.io/get-started/chat/
I would not mess with solution 2 unless the functionality is super complicated and requires acknowledgement from the client as well. In that case you could still use solution 3.
Related
So I was thinking about learning about app development for android. I know you use kotlin however, I also want to start working with .NET and C# is there a possibility for my first app that I create a basic login and register form in the app using Kotlin and connect it to a .NET REST API? Is that a thing I am sure you are just using the URL for the API call?
Sure! Using Retrofit, the Android app could be connected to the RESTful APIs that is available using the latest technology by Microsoft and the open source community; ASP.NET Core Web API 5.
A complete guide to do so:
http://codingsonata.com/a-complete-tutorial-to-connect-android-with-asp-net-core-web-api/
yes it is possible, there are plenty of libraries that are able to help you with that, like Retrofit or Volley
Yes. It's a very common way to consume API for data processing purposes in android-based applications.
If you have learnt on how to consume API with Android Application (built using JAVA), it's pretty much the same.
if you have never used JAVA to create android applications that consume API, don't worry because the process you need to do is very simple, moreover there are already many collections of libraries that you can use for this purpose. You can try to look on several library such as Retrofit and OkHttp.
Let me show you a a simple example of using OkHttp to pass data from android to API. This simple example is quoted from this article
private val client = OkHttpClient()
fun run() {
val formBody = FormBody.Builder()
.add("search", "Jurassic Park") /*The parameters*/
.build()
val request = Request.Builder()
.url("https://en.wikipedia.org/w/index.php") /*API URL*/
.post(formBody)
.build()
client.newCall(request).execute().use { response ->
if (!response.isSuccessful) throw IOException("Unexpected code $response")
println(response.body!!.string())
}
}
I hope this answer can be helpful for you. If you need more assistance, don't hesitate to contact.
If you are using API, you use URL and model of your data.
I recommend Retrofit as a library for connecting to any API. A good example is here, check out this and I think you will be well prepared to write Kotlin code.
Firstly, you need to define retrofit client, with your URL:
retrofit = new Retrofit.Builder()
.baseUrl("https://reqres.in")
.addConverterFactory(GsonConverterFactory.create())
.client(client)
.build();
then you need to map your API as an interface, for example if you have an endpoint https://reqres.in/api/users? you must define them like this:
#GET("/api/users?")
Call<UserList> doGetUserList(#Query("page") String page);
For creating .NET Core API, especially with user registration you must check some of tutorials, I recommend to be familiar with EF Identity (other).
I am new to using Sendgrid emails using c# .Net library. Our requirements wants us to track the status of the email like Delivered/Went to Spam/Client opened/reported as spam etc., By looking at the documentations and answers from other users to my previous questions its my understanding that there is no direct way to track the status of the email (like result object).
It would be really helpful if someone can point me to some example/sample codes or documentation/implementation in C# for the following
1) Adding unique parameters while sending the email using send grid API. Can I use a Guid string as my argument
I am assuming what I am doing below is correct.
var myMessage = new SendGridMessage();
var identifiers = new Dictionary<String, String>();
identifiers["Email_ID"] = "Email_ID";
identifiers["Email_Key"] = "9ebccd0d-67c0-4c28-bbf3-83d5bb69f098";
myMessage.AddUniqueArgs(identifiers);
2) How to use event webhooks to get the status with the unique argument that I used above from the http_post so that I can associate an email to the status. Any sample code , documentation in c# or an overall idea of how this works will get me started on this.
Appreciate your time and answers.
Sending emails via SendGrid is easier from C# using the official library that SendGrid provides. From your code example, it looks like you may already be using this - good job.
The unique argument should work as long as its been stringified, and you're not trying to pass an object to myMessage.AddUniqueArgs.
The Event Webhook will send a JSON packet to any URL that you specify. If you have included unique arguments in an email that you send out via SendGrid then these are automatically added to each event response you get back from the webhook - you don't need to turn anything else on to get the arguments as well.
There is an example of this call and the resulting response in the SendGrid Documentation.
SendGrid has an Event Webhook which posts events related to your email
activity to a URL of your choice. This is an easily deployable
solution that allows for customers to easiy get up and running
processing (parse and save) their event webhooks.
This is docker-based solution which can be deployed on cloud services
like Heroku out of the box.
https://github.com/sendgrid/sendgrid-csharp/tree/main/examples/eventwebhook/consumer
I've searched some time, looking for easy way to connect with some other sites WebAPI. There are some solutions, but they are made in very complicated way.
What I want to do:
Connect with server using URL adress
Provide login and password to get some data
Get data as JSON/XML
Save this data in an "easy-to-read" way. I mean: save it to C# variable which could be easy to modify.
Currently, API that I want to work with is Bing Search, but I'm looking for some universal way. I found an example, but it doesn't work for me and in my app I can't use this class: "DataServiceQuery" because it doesn't exsist.
How do you usually do it? Do you have your favourite solutions? Are there some universal ways or it depends on type of API that you work with?
I'm currently working on .NET MVC app (in case it could make any difference)
From server side
You can use that like below.
// Create an HttpClient instance
HttpClient client = new HttpClient();
// Send a request asynchronously continue when complete
client.GetAsync(_address).ContinueWith(
(requestTask) =>
{
// Get HTTP response from completed task.
HttpResponseMessage response = requestTask.Result;
// Check that response was successful or throw exception
response.EnsureSuccessStatusCode();
// Read response asynchronously as JsonValue
response.Content.ReadAsAsync<JsonArray>().ContinueWith(
(readTask) =>
{
var result = readTask.Result
//Do something with the result
});
});
You can see example on following link.
https://code.msdn.microsoft.com/Introduction-to-HttpClient-4a2d9cee
For JavaScirpt:
You could use jQuery and WebAPI both together to do your stuff.
There are few steps to it.
Call web api with Ajax jquery call.
Get reponse in JSON
Write javascript code to manipulate that response and do your stuff.
This is the easiest way.
See following link for reference:
http://www.codeproject.com/Articles/424461/Implementing-Consuming-ASP-NET-WEB-API-from-JQuery
It entirely depends on the type of API you want to use. From a .Net point of view, there could be .Net 2 Web Services, WCF Services and Web API Services.
Web APIs today are following the REST standard and RMM. Some APIs need API Keys provided as url parameters, others require you to put in request's header. Even some more robust APIs, use authentication schemes such as OAuth 2. And some companies have devised their own standards and conventions.
So, the short answer is that there is no universal way. The long answer comes from documentation of each API and differs from one to another.
I have recently returned to .net programming after a 7 year break.
I need to learn how to write a project within an existing open source asp.net mvc 5 ecommerce solution to receive posted json strings from a remote server running php with cURL, send acknowledgement responses, create my own json strings to post back to the remote server and receive acknowledgement responses. This must all be done with server side code, with no client side component whatsoever.
Serializing and deserializing json is not the issue, its using the correct kind of pages or services to send and receive json on the server without any client component, and using http objects directly. I have no experience or knowledge of creating this kind of project.
This is my question: I have had a look at a couple of tutorials about using .ashx and httpClient and httpContext but found them a little confusing. I would like to find a comprehensive guide about how to use json to communicate server to server with realistic examples. Is there one available?
Sounds like a perfect use case for WebApi. It is made specifically to work with JSON (or XML) requests and should work fine with requests issued by other scripts (not browsers).
There are plenty of tutorials available. Here is the official introduction tutorial.
What I often do to create JSON in C# is make an object/class which I serialize to JSON.
My controller has an function:
public JsonResult FunctionName()
{
var json = new { x1 = 10, y1 = "Hello" };
return Json(json);
}
You can call that function with PHP.
I am creating a PHP website connected to a MySQL database. Next I will need to write a C# desktop app that will use the same DB. Unfortunately I cannot connect to the DB directly from a remote location and my hosting company won't allow SSH neither.
So what options do I have? If the hosting company supported .NET, it wouldn't be a problem, but I'm not that experienced with PHP. Will I have to write a PHP service (SOAP?) and then consume it in my desktop app? Also, how do I communicate with server from the desktop app?
Any help appreciated!
Depending on security requirement, could you write a generic SQL executing page in PHP, that took the SQL as a String parameter, and returned the results as an array of Strings (Might need some meta data too or something)?
Other than that the only thing I can think of is a web service of some kind.
Also SOAP can work both ways, you can read and write from the C# app, no need to write a WebService on both ends, unless you need to notify your c# app about something from the server (In which case you could always try frequent polling from the c# app)
Best option would be creating a set of RESTful services in your PHP site.
One of most important things to take in account is REST is more configuration by convention, and there's no need of things like SOAP which may be an absolute overkill for your solution.
You just send JSON from PHP and .NET Windows application will parse it as a CLR object.
A sample scenario would be:
Service operation: http://yourdomainhere.com/API/Message/34894
** This returns something like { "text": "hello world" }
.NET client receives this JSON and using a JSON parser like Newton JSON parser, you'd be doing this:
MessageDto dto = JsonConvert.DeserializeObject([JSON received from the service call]);
MessageBox.Show(dto.Text); // This will show "hello world"
It's just a very simple example, but it'd give you an idea of what's next.
You can query your REST API using WebRequest/WebResponse .NET BCL classes.
PHP only needs to send a web response including your JSON in the output stream, that's all. No SOAP, no XML, no complication. Keep it simple.
I think the following link will be of helpful to you!
Developing SOAP Web Services with PHP/C#
What you can do is providing some PHP-wrappers which you can access from your C# code. As an example you can use this discussion, regarding C# / PHP communication.
Basically you can send a HTTP request to PHP and retrieve it's return value with C#. PHP would then perform the DB requests. If you're using AJAX on the Website it should be easy using the same communication interfaces.
this is the first paragraph of Matt Fellows answer.
But in what form do you send the data back to the application in?
Maybe JSON?
PHP webpage
<?php
$host = "host.host.com";
$user = "XXXXX";
$password = "XXXX";//plaintext :)
$connection = mysql_connect($host, $user, $password);
$database = "XXXXX";
$syntax = $_GET['syntax']; //www.example.com/help.php?syntax=DROP%20TABLE%20XXX
$result = mysql_query($syntax);
//somehow output the $result in C# readable form
?>