Guest User

Untitled

a guest
Mar 23rd, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.44 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Net;
  5. using System.Runtime.Serialization.Formatters.Binary;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using Google.Apis.Auth.OAuth2;
  9. using Google.Apis.Storage.v1.Data;
  10. using Google.Cloud.Storage.V1;
  11. using Microsoft.Bot.Builder.Dialogs;
  12. using Microsoft.Bot.Connector;
  13.  
  14. namespace GoogleCloudBot.Dialogs
  15. {
  16. [Serializable]
  17. public class RootDialog : IDialog<object>
  18. {
  19. public Task StartAsync(IDialogContext context)
  20. {
  21. context.Wait(MessageReceivedAsync);
  22.  
  23. return Task.CompletedTask;
  24. }
  25.  
  26. private async Task MessageReceivedAsync(IDialogContext context, IAwaitable<IMessageActivity> result)
  27. {
  28. var activity = await result;
  29.  
  30. // return our reply to the user
  31. await context.PostAsync("Hi. I am a chat bot developed using Microsoft Bot Framework and running on Google Cloud Platform!");
  32. try
  33. {
  34. PromptDialog.Choice<string>(context, OnUserChoiceMade, new List<string> { "Upload an image to Google Cloud Storage", "View a random image from Google Cloud Storage" },
  35. "Please select your choice");
  36. }
  37. catch (TooManyAttemptsException)
  38. {
  39. await context.PostAsync("Oops! You tried too many times.");
  40. }
  41. }
  42.  
  43. private async Task OnUserChoiceMade(IDialogContext context, IAwaitable<string> result)
  44. {
  45. var choice = await result;
  46. if (choice.Contains("Upload"))
  47. {
  48. await context.PostAsync("Please select an image to upload.");
  49. context.Wait(GetImageAsync);
  50. }
  51. else if (choice.Contains("View"))
  52. {
  53. try
  54. {
  55. //You don't need to pass credential when deploying to Compute Engine as it picks up the credential automatically.
  56. //Your code will look like this - var client = StorageClient.Create();
  57. var credential = GoogleCredential.FromFile(@"PATHTOKEY.json");
  58. var client = StorageClient.Create(credential);
  59. Bucket bucket = null;
  60. bucket = await SetUpGCP(client, bucket);
  61. bool noImagePresent = true;
  62. IMessageActivity message = context.MakeMessage();
  63.  
  64. foreach (var obj in client.ListObjects(bucket.Name))
  65. {
  66. noImagePresent = false;
  67. message.Attachments.Add(new Attachment()
  68. {
  69. ContentUrl = obj.MediaLink,
  70. ContentType = obj.ContentType
  71. });
  72. }
  73.  
  74. await context.PostAsync(message);
  75.  
  76. if (noImagePresent)
  77. {
  78. message.Text = "Sorry. No images uploaded yet.";
  79. await context.PostAsync(message);
  80. }
  81. }
  82. catch (Exception ex)
  83. {
  84. Console.WriteLine(ex.Message);
  85. }
  86.  
  87. context.EndConversation("See you soon");
  88. }
  89. }
  90.  
  91. private static async Task<Bucket> SetUpGCP(StorageClient client, Bucket bucket)
  92. {
  93. try
  94. {
  95. bucket = await client.CreateBucketAsync("YOURPROJECTID", "BUCKETNAME", new CreateBucketOptions { PredefinedAcl = PredefinedBucketAcl.PublicReadWrite });
  96. }
  97. catch (Google.GoogleApiException e)
  98. when (e.Error.Code == 409)
  99. {
  100. // The bucket already exists. That's fine.
  101. bucket = await client.GetBucketAsync("BUCKETNAME");
  102. }
  103.  
  104. return bucket;
  105. }
  106.  
  107. private async Task GetImageAsync(IDialogContext context, IAwaitable<IMessageActivity> result)
  108. {
  109. try
  110. {
  111. var message = await result;
  112. if (message.Attachments.Count == 0)
  113. await context.PostAsync("You should send an image file.");
  114. else
  115. {
  116. //You don't need to pass credential when deploying to Compute Engine as it picks up the credential automatically.
  117. //Your code will look like this - var client = StorageClient.Create();
  118. var credential = GoogleCredential.FromFile(@"PATHTOKEY.json");
  119. var client = StorageClient.Create(credential);
  120. Bucket bucket = null;
  121. bucket = await SetUpGCP(client, bucket);
  122. // Upload some files
  123. using (WebClient webClient = new WebClient())
  124. {
  125. using (Stream stream = webClient.OpenRead(message.Attachments[0].ContentUrl))
  126. {
  127. await client.UploadObjectAsync(bucket.Name, message.Attachments[0].Name, message.Attachments[0].ContentType, stream, new UploadObjectOptions { PredefinedAcl = PredefinedObjectAcl.PublicRead });
  128. }
  129. }
  130.  
  131. await context.PostAsync("Image Uploaded successfully");
  132. }
  133. }
  134. catch (Exception ex)
  135. {
  136. Console.WriteLine(ex.Message);
  137. }
  138. context.EndConversation("See you soon");
  139. }
  140. }
  141. }
Add Comment
Please, Sign In to add comment