Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Net;
- using Alexa.NET.Request;
- using Alexa.NET.Request.Type;
- using Alexa.NET.Response;
- using Amazon.Lambda.Core;
- using Newtonsoft.Json;
- // Assembly attribute to enable the Lambda function's JSON input to be converted into a .NET class.
- [assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.Json.JsonSerializer))]
- namespace LambdaJokes
- {
- //public class Jokes
- //{
- // public class Value
- // {
- // public int id { get; set; }
- // public string joke { get; set; }
- // public List<string> categories { get; set; }
- // }
- // public class RootObject
- // {
- // public string type { get; set; }
- // public Value value { get; set; }
- // }
- //}
- public class Function
- {
- public SkillResponse FunctionHandler(SkillRequest input, ILambdaContext context)
- {
- SkillResponse response = new SkillResponse();
- response.Response = new ResponseBody();
- response.Response.ShouldEndSession = false;
- IOutputSpeech innerResponse = null;
- var log = context.Logger;
- log.LogLine($"Skill Request Object:");
- log.LogLine(JsonConvert.SerializeObject(input));
- if (input.GetRequestType() == typeof(LaunchRequest))
- {
- log.LogLine($"Default LaunchRequest made");
- innerResponse = new PlainTextOutputSpeech();
- (innerResponse as PlainTextOutputSpeech).Text = $"Ciao, ecco la tua barzelletta: {EmitNewJokes()}";
- response.Response.ShouldEndSession = true;
- }
- else if (input.GetRequestType() == typeof(IntentRequest))
- {
- var intentRequest = (IntentRequest)input.Request;
- switch (intentRequest.Intent.Name)
- {
- case "AMAZON.CancelIntent":
- log.LogLine($"AMAZON.CancelIntent: send StopMessage");
- innerResponse = new PlainTextOutputSpeech();
- (innerResponse as PlainTextOutputSpeech).Text = "Fermato";
- response.Response.ShouldEndSession = true;
- break;
- case "AMAZON.StopIntent":
- log.LogLine($"AMAZON.StopIntent: send StopMessage");
- innerResponse = new PlainTextOutputSpeech();
- //(innerResponse as PlainTextOutputSpeech).Text = "Stopping";
- (innerResponse as PlainTextOutputSpeech).Text = "Ciao";
- response.Response.ShouldEndSession = true;
- break;
- case "AMAZON.HelpIntent":
- log.LogLine($"AMAZON.HelpIntent: send HelpMessage");
- innerResponse = new PlainTextOutputSpeech();
- (innerResponse as PlainTextOutputSpeech).Text = "Ciao per usare questa skill basta dire dammi una barzelletta oppure dimmi una barzelletta";
- break;
- case "RichiestaBarzelletta":
- log.LogLine($"Say Hello");
- innerResponse = new PlainTextOutputSpeech();
- (innerResponse as PlainTextOutputSpeech).Text = $"Ecco la tua barzelletta: {EmitNewJokes()}";
- response.Response.ShouldEndSession = true;
- break;
- default:
- log.LogLine($"Unknown intent: " + intentRequest.Intent.Name);
- innerResponse = new PlainTextOutputSpeech();
- (innerResponse as PlainTextOutputSpeech).Text = "Richiesta sconosciuta";
- break;
- }
- }
- response.Response.ShouldEndSession = true;
- response.Response.OutputSpeech = innerResponse;
- response.Version = "1.0";
- log.LogLine($"Skill Response Object...");
- log.LogLine(JsonConvert.SerializeObject(response));
- return response;
- }
- private static string EmitNewJokes()
- {
- try
- {
- //String json = new WebClient().DownloadString("http://api.icndb.com/jokes/random");
- //Jokes.RootObject jobject = JsonConvert.DeserializeObject<Jokes.RootObject>(json);
- //return jobject.value.joke;
- //https://pastebin.com/raw/YWaCv2hT
- String[] linee = new WebClient().DownloadString("https://gist.githubusercontent.com/MicheleRago/b73f1c86d84674a21c6dbd35ea97e1df/raw/58e960f21884f028d18527f3930fb4abc9f822c6/gistfile1.txt").Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
- List<string> listabarzellette = new List<string>();
- string appo = "";
- foreach (string s in linee)
- if (s == "FINE")
- {
- listabarzellette.Add(appo);
- appo = "";
- }
- else appo += s;
- int r = new Random().Next(listabarzellette.Count);
- return listabarzellette[r];
- }
- catch (Exception ex)
- {
- return "Error";
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement