Before you can get started, you need to do the following:
Set all Conversation API configuration settings.
- The latest version of .Net Core with Long Term Support and a familiarity with how to create a new console application.
Using the Conversation API, you can send messages to any channel you have configured. This tutorial shows you how to set up and send a message in a .Net Core application.
Create a new folder where you want to keep your app project. Then, open a terminal or command prompt to that location.
Create a new .Net Core console app with the following command:
dotnet new consoleAdd the
Newtonsoft.JsonNuGet package.dotnet add package Newtonsoft.Json
This tutorial uses basic authentication for testing purposes. We recommend OAuth 2.0 authentication in a production environment. Read more about authentication methods.
Open the
Program.csfile in your project folder. Replace all of the code in theMainmethod with the following code:SendMessage sendMessage = new SendMessage("App_Id", "Channel", "Identity"); sendMessage.send(sendMessage, "Access_Key", "Access_Secret", "Project_Id"); Console.ReadLine();Assign your values to the following parameters:
Parameter Your value App_IdFind your app ID on your Sinch [dashboard](https://dashboard.sinch.com/convapi/apps). Access_KeyFind your access key on your Sinch [dashboard](https://dashboard.sinch.com/settings/access-keys). Access_SecretFind your access secret on your Sinch [dashboard](https://dashboard.sinch.com/settings/access-keys).
Note:Access secrets are only available during initial key creation.Project_IdFind your project ID on your Sinch [dashboard](https://dashboard.sinch.com/settings/project-management). ChannelThe channel you want to use to send the message. Available channels are configured for the app on your Sinch [dashboard](https://dashboard.sinch.com/settings/access-keys). This guide assumes you've started with an SMSchannel, but you can use any channel configured for your app:SMSMESSENGERMMSRCSWHATSAPPVIBERVIBERBMINSTAGRAMTELEGRAMKAKAOTALKAPPLEBCLINE
IdentityThe ID of the contact to which you want to send the message. Save the file.
Next, create a new file in the project folder named
Message.cs. Populate that file with the "Message.cs" code found on this page and save the file. This code sends a text message.Message.cs// Find your App ID at dashboard.sinch.com/convapi/apps // Find your Project ID at dashboard.sinch.com/settings/project-management // Get your Access Key and Access Secret at dashboard.sinch.com/settings/access-keys using System; using System.Net.Http; using System.Text; using Newtonsoft.Json; public class Recipient { public Identified_By identified_by {get; set;} public Recipient(string channel, string identity) { identified_by = new Identified_By(channel, identity); } } public class Identified_By { public Channel_Identity[] channel_identities {get; set;} public Identified_By(string channel, string identity) { channel_identities = new Channel_Identity[] { new Channel_Identity(channel, identity) }; } } public class Channel_Identity { public string channel {get; set;} public string identity {get; set;} public Channel_Identity(string channelVar, string idVar) { channel = channelVar; identity = idVar; } } public class Message { public Text_Message text_message {get; set;} public Message() { text_message = new Text_Message(); } } public class Text_Message { public string text {get; set;} public Text_Message() { text = "Text message from Sinch Conversation API."; } } public class SendMessage { public string app_id {get; set;} public Recipient recipient {get; set;} public Message message {get; set;} public SendMessage(string appIdVar, string channel, string identity) { app_id = appIdVar; recipient = new Recipient(channel, identity); message = new Message(); } public string encodeAuth(string key, string secret) { var plainTextBytes = Encoding.UTF8.GetBytes(key + ":" + secret); return Convert.ToBase64String(plainTextBytes); } public async void send(SendMessage sendMessage, string key, string secret, string projectId) { using (var client = new HttpClient()) { client.DefaultRequestHeaders.Add("Authorization", "Basic " + encodeAuth(key, secret)); string json = JsonConvert.SerializeObject(sendMessage); var postData = new StringContent(json, Encoding.UTF8, "application/json"); var request = await client.PostAsync("https://us.conversation.api.sinch.com/v1/projects/" + projectId + "/messages:send", postData); var response = await request.Content.ReadAsStringAsync(); Console.WriteLine(response); } } }Note:This sample code is configured for the US region. If your Conversation API app wasn't created in the US region, replace all instances of
https://us.conversation.api.sinch.comwithhttps://eu.conversation.api.sinch.comin the sample code.Before executing your code, you must first compile your application. Execute the following command:
dotnet build
Now you can execute the code and send your test message. Run the following command:
dotnet runYou should receive a message in your configured messaging platform.
Now that you know how to send a message, learn how to handle an incoming message.
Read the links below to learn more: