Telegram C# example send message - c#

I can't find an example of sending message by telegram protocol from C#. I tried to use this but failed.
Can you give me any examples?

TLSharp is basic implementation of Telegram API on C#. See it here https://github.com/sochix/TLSharp

You can use the WTelegramClient library to connect to Telegram Client API protocol (as a user, not a bot)
The library is very complete but also very easy to use. Follow the README on GitHub for an easy introduction.
To send a message to someone can be as simple as:
using TL;
using var client = new WTelegram.Client(); // or Client(Environment.GetEnvironmentVariable)
await client.LoginUserIfNeeded();
var result = await client.Contacts_ResolveUsername("USERNAME");
await client.SendMessageAsync(result.User, "Hello");
//or by phone number:
//var result = await client.Contacts_ImportContacts(new[] { new InputPhoneContact { phone = "+PHONENUMBER" } });
//client.SendMessageAsync(result.users[result.imported[0].user_id], "Hello");

For my bot I use Telegram.Bot nuget package. Full sample code is here.
Here is example of sending message in reply to incoming message.
// create bot instance
var bot = new TelegramBotClient("YourApiToken");
// test your api configured correctly
var me = await bot.GetMeAsync();
Console.WriteLine($"{me.Username} started");
// start listening for incoming messages
while (true)
{
//get incoming messages
var updates = await bot.GetUpdatesAsync(offset);
foreach (var update in updates)
{
// send response to incoming message
await bot.SendTextMessageAsync(message.Chat.Id,"The Matrix has you...");
}
}

The simplest way is to send http request directly to the Telegram BOT API as url string, you may test those url strings even in your browser, please see details in my another answer here:
https://stackoverflow.com/a/57341990/11687179

at the first step you have to generate a bot in botfather then use the code in bellow in C#
private void SendMessage(string msg)
{
string url = "https://api.telegram.org/{botid}:{botkey}/sendMessage?chat_id={#ChanalName}&text={0}";
WebClient Client = new WebClient();
/// If you need to use proxy
if (Program.UseProxy)
{
/// proxy elements are variable in Program.cs
Client.Proxy = new WebProxy(Program.ProxyUrl, Program.ProxyPort);
Client.Proxy.Credentials = new NetworkCredential("hjolany", "klojorasic");
}
Client.DownloadString(string.Format(url, msg));
}));
}

Telegram has an official API that can do exactly what you need, you will have to look into http requests though..
Here is the documentation on sending a message:
Function
messages.sendMessage
Params
peer InputPeer User or chat where a message will be sent
message string Message text
random_id long Unique client message ID required to prevent message resending
Query example
(messages.sendMessage (inputPeerSelf) "Hello, me!" 12345678901)
Return errors
Code Type Description
400 BAD_REQUEST PEER_ID_INVALID Invalid peer
400 BAD_REQUEST MESSAGE_EMPTY Empty or invalid UTF8 message was sent
400 BAD_REQUEST MESSAGE_TOO_LONG Message was too long.
Current maximum length is 4096 UTF8 characters
For the full documentation go here.

Related

Can I verify if message is sent successfully using Response<SendReceipt> in Azure Storage Queue

I am using the Azure Storage Queue Client to send base 64 encoded string messages. A very straight forward usage. I have the following code snippet:
var options = new QueueClientOptions
{
MessageEncoding = QueueMessageEncoding.Base64
};
var _queueClient = new QueueClient(settings.Value.ConnectionString, settings.Value.Name, options);
var message = JsonConvert.SerializeObject(messageObject, jsonSetting);
_queueClient.SendMessageAsync(message);
I know that SendMessageAsync( . . .) returns Response<SendReceipt> how do I use that to verify if a message is sent successfully or not ?
SendReceipt has MessageId and InsetionTime properties but the documentation does not specify what happens to these properties if message does not get sent. Does the queue simply returns null receipt or an object with default values ?
When you try to send a message to a queue using SendMessageAsync and if for some reason message is not sent, an exception is raised. You will not receive anything in the Response<SendReceipt>.
You will need to wrap your await _queueClient.SendMessageAsync(message); in a try/catch block. If no exception is raised that would mean the message is sent successfully.

Delete a discord message with Webhook C#

How can I delete a discord message with a Webhook? I already added a Webhook send message but I cant find out how to delete the messages that the Webhook sent without opening the app and deleting it manually.
If you're using Discord.Net then deleting a message is handled pretty easily like this:
var sent = await Context.Channel.SendMessageAsync("Hello world!");
await sent.DeleteAsync();
If you are using the DSharpPlus library, the process of deleting a message from a Webhook is very simple!
Just store the value that the method ExecuteAsync(); returns in a variable and then use the method DeleteAsync();
As in this code:
//Here I am creating a Webhook that will send the message "Message to Delete"
DiscordWebhookBuilder WebhookSample = new DiscordWebhookBuilder
{
Username = "Username",
Content = "Message to Delete",
};
//Here I am getting the bot's main webhook in the current channel
DiscordWebhook currentChannelWebhook = await ctx.Channel.CreateWebhookAsync("Sample");
//Here I make the Webhook send a message and save the value
Task<DiscordMessage> result = await currentChannelWebhook.ExecuteAsync(WebhookSample);
//And finally, I delete the message sent
await result.DeleteAsync();

Twilio Error - 12200 - Schema validation warning - Content is not allowed in prolog

I have a webHook receiver that listens to Twilio POST. Scenario: SMS message is sent to my Twilio number, Twilio does POST to the webHook receiver, webHook processes the request (works as expected) and finally WebHook returns back a response object Twilio.TwiML.MessagingResponse. The problem is I'm receiving a warning in the Twilio Debugger with message "Content is not allowed in prolog." The warning shows in the REQUEST section of the Request Inspector and didn't know what to do about it.
screenshot of error/warn message
Thanks all for looking into this. Answer by #marcos-placona in here made me revisit the webHook return type. Sure enough the return type needs to be a TwiML formatted XML. That led to the discovery of the overloaded ToString() extension method.
public string ToString([System.Xml.Linq.SaveOptions formattingOptions = System.Xml.Linq.SaveOptions.None])
Member of Twilio.TwiML.TwiML
Summary:
Generate XML string from TwiML object
Parameters:
formattingOptions: Change generated string format.
for this to work, [System.Xml.Linq] needs to be referenced.
using Twilio.TwiML;
var twilioResponse = new MessagingResponse();
var message = new Twilio.TwiML.Messaging.Message("Thanks for your response.");
twilioResponse.Append(message);
return twilioResponse.ToString();
Hope this helps someone.

Notification Hub Delivery failure C#

I have two notification hub clients (nodejs and C#), both used for pushing messages into a hub.
The Node client sends perfectly fine, yet the C# client completes with no message being sent.
Below are the snippets for the used in each client.
C#
NotificationHubClient hub = NotificationHubClient.CreateClientFromConnectionString("<Connection String>", "<Hub Name>");
var notice = #"{'aps':{'alert':'Notification Hub test notification'}}";
var result = await hub.SendAppleNativeNotificationAsync(notice, "<tag>");
Console.WriteLine(result.State);
NodeJS
var azure = require('azure');
var notificationHubService = azure.createNotificationHubService('<Hub Name>','<Connection String>')
var notice = "{'aps':{'alert':'Notification Hub test notification'}}"
notificationHubService.apns.send("<tag>", notice, function(error, res){
console.log(res);
});
Both work fine when sending Android notifications and messages sent directly from the Azure portal Test feature are fine.
Any assistance would be greatly appreciated.
Duncan,
I think the issue is because your payload is malformed as you are using single quotes. Try the following:
var notice = "{\"aps\":{\"alert\":\"Notification Hub test notification\"}}";
You can try to use the EnableTestSend feature and look at the NotificationOutcome property for the detailed error message. This will send a test send message to 10 devices.
bool enableTestSend = true;
NotificationHubClient hub = NotificationHubClient.CreateClientFromConnectionString(connString, hubName, enableTestSend);
var outcome = await hub.SendWindowsNativeNotificationAsync(toast);
Console.WriteLine(outcome.State);
foreach (RegistrationResult result in outcome.Results)
{
Console.WriteLine(result.ApplicationPlatform + "\n" + result.RegistrationId + "\n" + result.Outcome);
}
TLDR - No '' (single quotes) allowed! Payload must containt alert key
Not a very exciting solution but...
If the notification payload contains single quotes even in a string literal the Notification hub will en-queue it without a malformed exception but when it reaches the APNS it will not be delivered as the payload is invalid according to them.
If the notification payload doesn't have alert key present it will also be en-queued but not delivered by the APNS
As already mentioned in my colleagues answers, you need to use double quotes in C# as a first step.
Second, you also need to escape the json characters:
Your payload should look like this:
var notice = "{{\"aps\":{{\"alert\":\"Notification Hub test notification\"}}}}";
Basically you escape the json braces { } by adding additional ones {{ }}.
This will send a valid json payload to the SendAppleNativeNotificationAsync() method. The payload now sent looks like this:
{"aps":{"alert":"Notification Hub test notification"}} (which is the correct notification format on iOS)
Here are the valid json payloads for iOS from the Apple Developer webpage.
You could always test if the json you are sending is a valid notification payload by using the 'Test Send' functionality in the Azure Notification Hub you are using.

how to setup Twilio

I'm trying to implement Twilio to my WP8 application so I can send messages, I have downloaded the API helper and I have tried implement the c# just to test if it works. I am only a beginner at programming and I don't really understand why it wont work.
class sendmessages
{
private static void Main(string[] args)
{
// Find your Account Sid and Auth Token at twilio.com/user/account
string AccountSid = "......"; //My own details
string AuthToken = "......."; //My own details
var twilio = new TwilioRestClient(AccountSid, AuthToken);
var message = twilio.SendSmsMessage("From" "TO", "hello ");
Console.WriteLine(message.Sid);
}
}
Is there something I'm missing to do cause the Twilio Website is not very helpful, as the documentation is not very clear. I don't want anyone to do it for me, any tutorials that would help would be much appreciated.
Twilio evangelist here.
Are you getting a specific error?
To start debugging this I'd suggest checking to make sure that the RestException property is null:
var message = twilio.SendSmsMessage("[YOUR_FROM_NUMBER]","[YOUR_TO_NUMBER]","[MESSAGE_BODY]");
if (message.RestException != null) {
//An error happened calling the REST API
Debug.Writeline(message.RestException.Message);
}
If RestException is null, I'd suggest checking the logs in the Twilio developer portal to see if Twilio reports sending a message.
If neither of those two things work, my next suggestion would be to use Fiddler to watch the actual HTTP request the library is making to the REST API and see what the result is.
Hope that helps.

Categories