Link formatting not working - c#

On the Slack website, I can format a message like this:
{
"text": "<http://google.com|link to google>"
}
And in the message it will appear like this:
link to google
But I am trying to write a bot and those links aren't working. Using my websocket connection I can send a message like this:
var send = new MessageToSlack()
{
type = "message",
channel = msg.channel,
text = $"http://google.com"
};
ws.SendAsync(JsonConvert.SerializeObject(send), b => { });
And Slack will correctly interpret http://google.com as a link and display it like:
http://google.com
But if I try to send a message with the link in angle brackets with the pipe between the link and the link text (which works on Slack's site) like this:
var send = new MessageToSlack()
{
type = "message",
channel = msg.channel,
text = $"<http://google.com|to google>"
};
ws.SendAsync(JsonConvert.SerializeObject(send), b => { });
I end up with:
<http://google.com|to google>
So how do I get this working from my bot? Why is it not able to parse by links correctly? What am I missing?
As far as I can see from the docs, this should work. Here in the section on formatting messages it says:
The RTM API only supports posting simple messages formatted using our default message formatting mode.
And links to here which does mention links with the pipe character, so I think it should work.
(note MessageToSlack is just an ordinary .NET class with type, channel and text properties that gets serialized by JSON.Net and appears to give the correct JSON. ws is my websocket connection from the WebSocketSharp nuget package)
JSON:
{
"id": 1,
"type": "message",
"channel": "C6QRKT0EA",
"text": "<http://google.com|to google>"
}
Edit: So it seems if I switch from replying with the web socket connection and instead post to https://slack.com/api/chat.postMessage, it will work correctly, but it's a bit more fiddly to use and the documentation led me to believe that the links should work without needing to jump through that particular hoop. Am I just misreading the docs? Or are docs just not very clear on this point?

Try to enable markdown support by adding "mrkdwn": true to your json
{
"type": "message",
"channel": "C6QRKT0EA",
"text": "<http://google.com|to google>",
"mrkdwn": true
}
Read Message Formatting section. Hope it will help.

Related

Pass custom debug information to Microsoft bot framework emulator

I am developing a bot based on a .NET Bot Builder SDK.
Is it possible for a bot to pass some debug information together with the message, so I can see it in the Details section of the Bot Framework Chanel Emulator when the message is clicked?
Great Question. Yes, it is entirely possible. You can use the ChannelData property of your activity you are responding with. The data entered into the ChannelData property must be valid JSON For example:
var reply = activity.CreateReply("test");
string json = #"{
CustomField1: 'Field one value',
CustomField2Array: [
'First Element',
'Second Element'
]
}";
reply.ChannelData = JObject.Parse(json);
await context.PostAsync(reply);
In the emulator this will appear as:
"channelData": {
"CustomField1": "Field one value",
"CustomField2Array": [
"First Element",
"Second Element"
]
}

How is Activity.Text encoded Microsoft Teams?

I am writing a custom Teams bot. I got some questions about Activity.Text field:
How is it encoded? I see some <at> tags when the bot is #mentioned, but I also see some '. Can I use HttpUtility.HtmlDecodeto decode it?
Is there any document about the details of the Activity.Text field? The schema says it is
Text of the message that is sent from user to bot or bot to user. See the channel's documentation for limits imposed upon the contents of this property.
But it does not talk about the details about the field.
Nothing special about Activity.Text in Teams except for the tags. In general, depending on the Activity.TextFormat, the message you send might contain markdown or XML, but in general the message you receive from a user will be plain text.
Anything additional, like if the user sends bold text to your bot, can be extracted from the attachments object in the incoming payload, e.g. :
"attachments": [
{
"contentType": "text/html",
"content": "<div><span itemscope=\"\" itemtype=\"http://schema.skype.com/Mention\" itemid=\"0\">Teams TestBot</span> |echo| <strong><strong>Hi</strong></strong>​​​​​​​</div>"
}
]

Access embedded images

We are using the GMail Api in a C# application, and have received a message with an embedded image.
You can see the image if you look at the GMail account (in a browser as a user) but I am unable to find it either in the Payload or get an attachment id when I use the api.
Does the api even support accessing embedded images?
If I use:
var part = message.Payload.Parts
.Where(x => x.MimeType.Equals(mimeType))
.Select(x => x.Body.Data)
.FirstOrDefault();
With the Mime type set to Gif,Jpeg or Tiff it always comes back null;
You might want to check the - Users.messages: get
Gets the specified message.
To be familiar on the structure specially when reading the email message Resource representations. I used the Try it now to get the Users.messages resource in the response body and how it will be formed.
When checking the email, the embedded image will fall in the payload.
........
{
"partId": "1",
"mimeType": "image/png",
"filename": "pasted1",
"headers": [
{
"name": "Content-Type",
"value": "image/png; name=pasted1"
},
.........
This is how it should look like.
Hope this helps.

Outlook Rest API sending mail with attachments

I've written an application that automates the sending of mail from our system via a small console app that i've designed to run in azure as a webjob. The app sends mail perfectly fine until I attempt to add a collection of attatchments. Referring to https://msdn.microsoft.com/office/office365/APi/mail-rest-operations#Createandsendmessages
I've attempted to send their sample code for this with my Attatchments collection containing a single object that looks like this
{
"#odata.type": "#Microsoft.OutlookServices.FileAttachment",
"Name": "menu.txt",
"ContentBytes": "bWFjIGFuZCBjaGVlc2UgdG9kYXk="
}
However, after this api request was rejected with the error:
{"error":{"code":"RequestBodyRead","message":"The property 'ContentBytes' does not exist on type 'Microsoft.OutlookServices.Attachment'. Make sure to only use property names that are defined by the type."}}
And furthermore spending half an hour on the outlook services support line to fobbed off with go checkout some forums and to read the article I'd been using for reference since I started the project I've thrown in the towel. If anyone can assist me in getting this api to accept the request I'd be eternally thankful.
This is what a sample request looks like.
POST https://outlook.office.com/api/v2.0/me/sendmail
{
"Message": {
"Subject": "Meet for lunch?",
"Body": {
"ContentType": "Text",
"Content": "The new cafeteria is open."
},
"ToRecipients": [
{
"EmailAddress": {
"Address": "garthf#a830edad9050849NDA1.onmicrosoft.com"
}
}
],
"Attachments": [
{
"#odata.type": "#Microsoft.OutlookServices.FileAttachment",
"Name": "menu.txt",
"ContentBytes": "bWFjIGFuZCBjaGVlc2UgdG9kYXk="
}
]
},
"SaveToSentItems": "false"
}
And I'm aware that the message object has a bool flag of HasAttachments which when set to true does not affect the outcome of the call.
Based on the error message, you were using the ‘Microsoft.OutlookServices.Attachment’ intead of ‘Microsoft.OutlookServices.FileAttachment’.
I can reproduce this issue when I use the ‘Microsoft.OutlookServices.Attachment’ too.
And the sample above which use ‘Microsoft.OutlookServices.FileAttachment’ works well for me.
Please ensure that you were using the ‘Microsoft.OutlookServices.FileAttachment’ to send the text attachment.

Example of creating a draft with an attachment in C#?

I've written a test application in C# that creates a draft message using the new Gmail API. It works fine when the message has no attachment.
I'm moving from the IMAP API and have used the MailBee.NET components with that API. The MailBee.NET components includes a class that produces an RFC 2822 message, so I've re-used this and have Base64-encoded the message and have assigned to the "Raw" property as described here:
https://developers.google.com/gmail/api/guides/drafts
MailMessage msg = new MailMessage();
msg.Subject = "test!";
msg.BodyPlainText = "Test content";
msg.Attachments.Add(#"D:\Trace.log", "Trace.log", Guid.NewGuid().ToString(), null, null, NewAttachmentOptions.Inline, MailTransferEncoding.Base64);
Message m = new Message();
m.Raw = Convert.ToBase64String(msg.GetMessageRawData());
Draft d = new Draft();
d.Message = m;
d = gs.Users.Drafts.Create(d, "me").Execute();
It works fine when no attachment is added, but fails with a 500 response when one is added:
{
"error": {
"errors": [
{
"domain": "global",
"reason": "backendError",
"message": "Backend Error"
}
],
"code": 500,
"message": "Backend Error"
}
}
Could somebody please provide an example of how to do this using the .NET API? The example on the API page is very barebones and doesn't really give much in the way of useful information and the documentation isn't great. It would probably be best to use the Message / MessagePart / MessagePartBody classes included with the .NET Client, however I can't find any clear guidance or examples on their use so don't know where to begin.
Questions:
1) Can anybody provide some example code of creating a draft message with an attachment using the classes within the .NET Client?
2) Is it possible to attach more than one file? The documentation refers to a single file throughout and the Multipart guidance refers to exactly two parts: metadata and attachment.
Providing a sample "raw" field that you're uploading would definitely be helpful to debug (either base64 encoded or just directly).
However this sounds related to:
GMail API : unable to add an attachment in a draft
also about this:
m.Raw = Convert.ToBase64String(msg.GetMessageRawData());
you want to make sure you're using "web safe" (aka "url safe") base64 encoding alphabet from https://www.rfc-editor.org/rfc/rfc4648#section-5
as it says in the docs at the URL you mentioned:
"""
Your application can create drafts using the drafts.create method. The general process is to:
Create a MIME message that complies with RFC 2822.
Convert the message to a URL-safe base64-encoded string.
Create a draft, setting the value of the drafts.message.raw field to the encoded string.
"""
Google APIs use the
Much like for the poster of the other question GmailGuy referred to, this has magically started working overnight. So it must've been a Gmail-side problem after all.
Regarding:
m.Raw = Convert.ToBase64String(msg.GetMessageRawData());
Thanks for the heads-up on this; I had actually encoded it previously but while trying 20 different things to get things working I removed it and forgot to add it back in!
Also, to confirm: yes, you're able to add more than one attachment when you use the raw message approach.

Categories

Resources