How to use Adaptive Cards on Teams Messaging Extension with thumbnail card preview? - azure

I want to send a card to a teams channel using messaging extension. On messaging extension i need to show a preview thumbnail card and onclick of that thumbnail a adaptive card will be displayed.
I have tried the below code and while trying to use "MessagingExtensionResult" its giving error. Also i'm unable to add the dll for "MessagingExtensionResult" its giving incompatible version error. I'm using .Net framework 4.6.
var results = new ComposeExtensionResult()
{
AttachmentLayout = "list",
Type = "result",
Attachments = new List<ComposeExtensionAttachment>(),
};
var card = CardHelper.CreateCardForExperties(pos, true);
var composeExtensionAttachment = card.ToAttachment().ToComposeExtensionAttachment();
results.Attachments.Add(new ComposeExtensionAttachment
{
ContentType = "application/vnd.microsoft.teams.card.adaptive",
Content = JsonConvert.DeserializeObject(updatedJsonString),
Preview = composeExtensionAttachment
});

Using below code we can invoke adaptive card from thumbnail card preview.
ComposeExtensionResponse response = null;
1. var results = new ComposeExtensionResult()
{
AttachmentLayout = "list",
Type = "result",
Attachments = new List<ComposeExtensionAttachment>(),
};
Create a function that returns thumbnail card (preview card)
var previewThumbnailCard = CreateThumbnailCard();
Create a function that returns Adaptive card in form of attachment.
var adaptivecardattachment = CreateAdaptiveCardAsAttachment();
Cast that attachment card to composeextensionattachment and pass
thumbnail card to it as attachment.
var composeExtensionAttachmentAdaptive = adaptivecardattachment .ToComposeExtensionAttachment(previewThumbnailCard.ToAttachment());
Return the response
{
ComposeExtension = results
};
return response;

Related

ComputerVisionClient or Xamarin Essentials Error - Invalid URI: The format of the URI could not be determined when calling method ReadInStreamAsync

So I am capturing a photo and opening a stream using Xamarin.Essentials 1.7 MediaPicker built into Essentials.
When I call the ReadInStreamAsync(stream) method in Computer Vision Client, I get an error and my Xamarin.Forms app breaks inside the method: 'Invalid URI: The format of the URI could not be determined.'
This is the stream.Name value - '/data/user/0/com.companyname.xamphotoappdemo2/cache/2203693cc04e0be7f4f024d5f9499e13/198fd32db9cc4be38a493325974fa138/d964251252fc4963aca94339d73a8007.jpg'
This is my code:
var file = await MediaPicker.CapturePhotoAsync(new MediaPickerOptions
{ Title = "Please take a photo" });
if(file != null)
{
var stream = await file.OpenReadAsync();
chosenImage.Source = ImageSource.FromStream(() => stream);
// 2. Add OCR logic.
var client = Authenticate(ApiSettings.subscriptionKey, ApiSettings.endpoint);
var text = await client.ReadInStreamAsync(stream);
//after the request, get the operation location
string operationLocation = text.OperationLocation;
//we only need the operation ID, not the whole URL
const int numberOfCharsInOperationId = 36;
string operationId = operationLocation.Substring(operationLocation.Length - numberOfCharsInOperationId);
//Get the ocr read results
ReadOperationResult results;
do
{
results = await client.GetReadResultAsync(Guid.Parse(operationId));
}
while ((results.Status == OperationStatusCodes.Running || results.Status == OperationStatusCodes.NotStarted));
var readResults = results.AnalyzeResult.ReadResults;
var expirationDates = from page in readResults
from line in page.Lines
where line.Text.Contains("EXPIRES") && line.Words.Count == 4
select line.Words[3].Text;
expirationDate.Text = expirationDates.ToString();
photoPath.Text = file.FullPath;
The image is displaying as expected in the XAML Image control and that is reading the image source from the stream, so is this a bug in the ReadInStreamAsync method?

Issue in attachment upload in BOT emulator of Bot framework

I am uploading an attachment in BOT emulator, after uploading an attachment I am converting it to base64, to pass it to our service.
I pick this attachment from path D:\Images\MobileRequest.PNG, but after uploading it to BOT app it shows the path of attachment as http://127.0.0.1:44185/v3/attachments/ne7djbemc9f40bifi/views/original/MobileRequest.PNG, as the image is not available on this path, So while converting the image to base64, it throws an error as "URI formats are not supported.".
How to get actual physical path i.e "D:\Images\MobileRequest.PNG" in BOT app.
Below is code from my BOT app
var dialog = new PromptDialog.PromptAttachment("Please attach screenshot ", "Sorry, I didn't get the attachment. Try again please.", 2);
context.Call(dialog, afterUpload);
private async Task afterUpload(IDialogContext context, IAwaitable<IEnumerable<Attachment>> result)
{
IEnumerable<Attachment> attach = await result;
string filePath = attach.FirstOrDefault().ContentUrl + "/" + attach.FirstOrDefault().Name;
context.UserData.SetValue("filePath", filePath);
}
string filePath = string.Empty;
context.UserData.TryGetValue("filePath", out filePath);
using (System.Drawing.Image image = System.Drawing.Image.FromFile(filePath))
{
using (MemoryStream m = new MemoryStream())
{
image.Save(m, image.RawFormat);
byte[] imageBytes = m.ToArray();
attach1 = Convert.ToBase64String(imageBytes);
}
}
Your bot will be deployed so you will not have access to local files.
You can easily convert your image located at a URL by doing the following:
using (var client = new HttpClient())
{
var bytes = await client.GetByteArrayAsync(imageUrl);
var imageInBase64String = "image/jpeg;base64," + Convert.ToBase64String(bytes);
// Do what you want with your converted image
}

Microsoft Bot Framework WebChat: Add bot image

How to add image of the bot with some welcome text in the middle in Microsoft Bot Framework Web Chat. Seems like quite common functionality and I see images which indicates that is possible.
Anyone knows how to add it?
you can use the below code and replace your image path to give response from bot to user including text and image.
await context.PostAsync("Here we go with the welcome message\n"+"![AN IMAGE!](Your_Image_URL)");
Another way is, you can also use Card functionality:
private async Task Greeting(IDialogContext context, IAwaitable<IMessageActivity> argument)
{
var message = await argument;
if (string.IsNullOrEmpty(message.Text))
{
// Hero Card
var cardMsg = context.MakeMessage();
var attachment = BotWelcomeCard("Hello,I am a bot.", "");
cardMsg.Attachments.Add(attachment);
await context.PostAsync(cardMsg);
}
else
{
// else code
}
}
private static Attachment BotWelcomeCard(string responseFromQNAMaker, string userQuery)
{
var heroCard = new HeroCard
{
Title = userQuery,
Subtitle = "",
Text = responseFromQNAMaker,
Images = new List<CardImage> { new CardImage("../img/bot.gif") },
Buttons = new List<CardAction> { new CardAction(ActionTypes.ImBack, "Show Menu", value: "Show Bot Menu") }
};
return heroCard.ToAttachment();
}
ok, here is what we end up doing:
<script>
$(document).ready(function () {
$(".wc-header").append("<div class='wc-header-welcome'><img src='/Images/bot.png'/><div>Hello! I am your bot</div>");
});
</script>
Hope it will help save time to someone else.

Default Media Receiver limitations

Can I use the Default Media Receiver to display a web page or an HTML5 app? Using Javascript in the Chrome browser, I have no problem sending a single png image (content type image/png) to the Chromecast but it fails if I specify an html link (content type text/html). session.loadMedia will fire the error handler and e.code/e.description reports session_error/LOAD_FAILED. I used Google's home page for my test:
//var currentMediaURL = "https://www.google.com/images/srpr/logo11w.png";
//var currentMediaType = "image/png";
var currentMediaURL = "https://www.google.com";
var currentMediaType = "text/html";
function startApp()
{
var mediaInfo = new chrome.cast.media.MediaInfo(currentMediaURL, currentMediaType);
var request = new chrome.cast.media.LoadRequest(mediaInfo);
session.loadMedia(request, onMediaDiscovered.bind(this, 'loadMedia'), onMediaError);
};
I think you need to have custom receiver, just have it run your code accordingly...

Retrieve public statistics of video via youtube api

It's possible to obtain public statistics of video?
Using something like this i can get just total views of video and like count:
https://www.googleapis.com/youtube/v3/videos?part=statistics&key=API_KEY&id=ekzHIouo8Q4
It's possible to get those public statistics?
I found this question
Youtube GData API : Retrieving public statistics
But maybe something has changed?
The only API call under Version 3 of the API that will get you statistics is the
youtube.videos.list API
Try this API Explorer link to try:
https://developers.google.com/apis-explorer/#p/youtube/v3/youtube.videos.list?part=snippet%252C+statistics&id=Ys7-6_t7OEQ&maxResults=50&_h=2&
You can get those using Analytics API
Sample requests would help you understand.
Analytics API is a different service but libraries come in same package and you can use same authorization with adding "https://www.googleapis.com/auth/yt-analytics.readonly" scope
You would need to create YouTubeService object and can get search results for the keywords
YouTubeService youtubeService = new YouTubeService(new BaseClientService.Initializer()
{
ApiKey = "dfhdufhdfahfujashfd",
ApplicationName = this.GetType().ToString()
});
var searchListRequest = youtubeService.Search.List("snippet");
searchListRequest.Q = "cute cats";
searchListRequest.MaxResults = 10;
var searchListResponse = await searchListRequest.ExecuteAsync();
var videoId = searchListResponse.Items.First().Id.VideoId is the unique id of the video
// Video Request
VideosResource.ListRequest request = new VideosResource.ListRequest(youTubeService, "statistics")
{
Id = videoId
};
VideoListResponse response = request.Execute();
if (response.Items.First() != null && response.Items.First().Statistics != null)
{
Console.WriteLine(response.Items.First().Statistics.ViewCount);
}

Resources