Advertisement
Guest User

Untitled

a guest
Aug 18th, 2019
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.42 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Net;
  4. using Alexa.NET.Request;
  5. using Alexa.NET.Request.Type;
  6. using Alexa.NET.Response;
  7. using Amazon.Lambda.Core;
  8. using Newtonsoft.Json;
  9.  
  10. // Assembly attribute to enable the Lambda function's JSON input to be converted into a .NET class.
  11. [assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.Json.JsonSerializer))]
  12.  
  13. namespace LambdaJokes
  14. {
  15. //public class Jokes
  16. //{
  17. // public class Value
  18. // {
  19. // public int id { get; set; }
  20. // public string joke { get; set; }
  21. // public List<string> categories { get; set; }
  22. // }
  23.  
  24. // public class RootObject
  25. // {
  26. // public string type { get; set; }
  27. // public Value value { get; set; }
  28. // }
  29. //}
  30.  
  31. public class Function
  32. {
  33. public SkillResponse FunctionHandler(SkillRequest input, ILambdaContext context)
  34. {
  35. SkillResponse response = new SkillResponse();
  36. response.Response = new ResponseBody();
  37. response.Response.ShouldEndSession = false;
  38. IOutputSpeech innerResponse = null;
  39. var log = context.Logger;
  40. log.LogLine($"Skill Request Object:");
  41. log.LogLine(JsonConvert.SerializeObject(input));
  42. if (input.GetRequestType() == typeof(LaunchRequest))
  43. {
  44. log.LogLine($"Default LaunchRequest made");
  45. innerResponse = new PlainTextOutputSpeech();
  46. (innerResponse as PlainTextOutputSpeech).Text = $"Ciao, ecco la tua barzelletta: {EmitNewJokes()}";
  47. response.Response.ShouldEndSession = true;
  48. }
  49. else if (input.GetRequestType() == typeof(IntentRequest))
  50. {
  51. var intentRequest = (IntentRequest)input.Request;
  52. switch (intentRequest.Intent.Name)
  53. {
  54. case "AMAZON.CancelIntent":
  55. log.LogLine($"AMAZON.CancelIntent: send StopMessage");
  56. innerResponse = new PlainTextOutputSpeech();
  57. (innerResponse as PlainTextOutputSpeech).Text = "Fermato";
  58. response.Response.ShouldEndSession = true;
  59. break;
  60. case "AMAZON.StopIntent":
  61. log.LogLine($"AMAZON.StopIntent: send StopMessage");
  62. innerResponse = new PlainTextOutputSpeech();
  63. //(innerResponse as PlainTextOutputSpeech).Text = "Stopping";
  64. (innerResponse as PlainTextOutputSpeech).Text = "Ciao";
  65. response.Response.ShouldEndSession = true;
  66. break;
  67. case "AMAZON.HelpIntent":
  68. log.LogLine($"AMAZON.HelpIntent: send HelpMessage");
  69. innerResponse = new PlainTextOutputSpeech();
  70. (innerResponse as PlainTextOutputSpeech).Text = "Ciao per usare questa skill basta dire dammi una barzelletta oppure dimmi una barzelletta";
  71. break;
  72.  
  73. case "RichiestaBarzelletta":
  74. log.LogLine($"Say Hello");
  75. innerResponse = new PlainTextOutputSpeech();
  76. (innerResponse as PlainTextOutputSpeech).Text = $"Ecco la tua barzelletta: {EmitNewJokes()}";
  77. response.Response.ShouldEndSession = true;
  78. break;
  79.  
  80.  
  81. default:
  82. log.LogLine($"Unknown intent: " + intentRequest.Intent.Name);
  83. innerResponse = new PlainTextOutputSpeech();
  84. (innerResponse as PlainTextOutputSpeech).Text = "Richiesta sconosciuta";
  85. break;
  86. }
  87. }
  88. response.Response.ShouldEndSession = true;
  89. response.Response.OutputSpeech = innerResponse;
  90. response.Version = "1.0";
  91. log.LogLine($"Skill Response Object...");
  92. log.LogLine(JsonConvert.SerializeObject(response));
  93. return response;
  94. }
  95.  
  96. private static string EmitNewJokes()
  97. {
  98. try
  99. {
  100. //String json = new WebClient().DownloadString("http://api.icndb.com/jokes/random");
  101. //Jokes.RootObject jobject = JsonConvert.DeserializeObject<Jokes.RootObject>(json);
  102. //return jobject.value.joke;
  103.  
  104. //https://pastebin.com/raw/YWaCv2hT
  105. String[] linee = new WebClient().DownloadString("https://gist.githubusercontent.com/MicheleRago/b73f1c86d84674a21c6dbd35ea97e1df/raw/58e960f21884f028d18527f3930fb4abc9f822c6/gistfile1.txt").Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
  106. List<string> listabarzellette = new List<string>();
  107. string appo = "";
  108. foreach (string s in linee)
  109. if (s == "FINE")
  110. {
  111. listabarzellette.Add(appo);
  112. appo = "";
  113. }
  114. else appo += s;
  115.  
  116. int r = new Random().Next(listabarzellette.Count);
  117. return listabarzellette[r];
  118. }
  119. catch (Exception ex)
  120. {
  121. return "Error";
  122. }
  123. }
  124. }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement