Access embedded images - c#

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.

Related

How to display saved Bot Transcripts on the bot? How to retrieve the right transcript for user?

I am trying to implement Blob transcript storage(https://learn.microsoft.com/en-us/azure/bot-service/bot-builder-howto-v4-storage?view=azure-bot-service-4.0&tabs=csharp#blob-transcript-storage-implementation). I am able to implement and see the stored transcripts in Azure blob storage under container as json files. Now the issue I am facing is how do I display them properly. If I use SendActivityasync then it displays both bot and user messages as if bot is typing it (on the bot side-left side).
Is there a way to dump the transcript in one go? Just to display the whole conversation or is it supposed to be done one by one? Is there a function that allows this?
Since these transcripts are saved using conversation ids, how do I use this sample to get only the user who's using the bot and not all? If the conversation ids are new for each conversation and user ids according to sample is generated using 'Guid.NewGuid()' it will be random everytime. Are these being saved to keep track of users? Is there any sample to see the whole thing?
I have seen a lot of samples mentioning this being used for debugging. Has anyone implemented this and if so what samples are you following?
Thanks
This is answered in this GitHub Issue. The original issue is pretty old, but there's an updated answer in the linked comment.
Basically, you need to:
Load all of your activities from storage (maybe use the Blob REST API for this)
Pass the activities in to the createStore() method
Pass the store into the renderWebChat() method. For example:
// This is just an example. You'll want to populate history with
// an array of activities from your storage.
const history = [
{
type: "message",
id: "******************",
timestamp: "2020-10-30T20:45:52.7120529Z",
channelId: "webchat",
from: {
id: "******************",
name: "******************",
role: "bot",
},
conversation: {
id: "******************",
},
locale: "en-US",
text: "THIS IS A PREVIOUS MESSAGE",
inputHint: "acceptingInput",
attachments: [
],
entities: [
],
replyToId: "******************",
}
]
[...]
const store = window.WebChat.createStore({ activities: history}, ({ dispatch }) => next => action => {
return next(action);
});
window.WebChat.renderWebChat({
directLine: window.WebChat.createDirectLine({ secret: token }),
store
},
document.getElementById('webchat'));
[...]

Link formatting not working

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.

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.

asp.net call url to get information

I'm trying to implement a domain search in our website, but I have no clue how... the WHOIS service I registered by, gave me an API key to use... When I use the following address in my browser's url,
http://api.robowhois.com/v1/availability/example.com
a login box appears, asking me for my username and password, when I type in my username and password which is the key they gave me, a page appears with the following
{
"response": {
"available": false
}
}
I'm sorry to say, but I've been searching for weeks on how to solve this but at the end my last resort was to turn to stack overflow... can someone please help, is there a way on how to use and call the url and use the info?
You already got the information you need. It responds with a JSON object saying it's not available.
To retrieve the information as you wish, you can use Jquery, just put your URL in a function as in the examples and get data.response.available value and assign it to your textbox etc. For more information how to make JSON calls and parse them, check out this documentation in Jquery website.
RoboWhois is a web service that provides an API suite to access WHOIS
records and domain related information with a unified, consistent
interface. Using RoboWhois API you can retrieve WHOIS details parsed
as convenient JSON structure.
In order to check the availability of a given domain you have to send a http get request to the robowhois api http://api.robowhois.com/v1/availability/example.com
The server does answer the request by sending http response containing json which looks like this:
{
"response": {
"available": false
}
}
which means the domain is no longer available.
In order to use the information contained in the json response you should deserialize the json object to a c# object. You can do this for example with the json.net library.
Here is a small example from the documentation on how to use json.net to deserialize a json:
Product product = new Product();
product.Name = "Apple";
product.ExpiryDate = new DateTime(2008, 12, 28);
product.Price = 3.99M;
product.Sizes = new string[] { "Small", "Medium", "Large" };
string output = JsonConvert.SerializeObject(product);
//{
// "Name": "Apple",
// "ExpiryDate": "2008-12-28T00:00:00",
// "Price": 3.99,
// "Sizes": [
// "Small",
// "Medium",
// "Large"
// ]
//}
Product deserializedProduct = JsonConvert.DeserializeObject<Product>(output);

Categories

Resources