From 5c1acaa8946b01d0746b1836d03d2d0e9f0575c4 Mon Sep 17 00:00:00 2001 From: pratikthorat Date: Wed, 21 Oct 2020 17:12:52 +0530 Subject: [PATCH 1/2] Added new user command for getting bitcoin price update --- Models/CryptoData.cs | 22 ++++++++++++++++++++++ Modules/UserCommands.cs | 36 +++++++++++++++++++++++++----------- 2 files changed, 47 insertions(+), 11 deletions(-) create mode 100644 Models/CryptoData.cs diff --git a/Models/CryptoData.cs b/Models/CryptoData.cs new file mode 100644 index 0000000..c7e418e --- /dev/null +++ b/Models/CryptoData.cs @@ -0,0 +1,22 @@ +using Newtonsoft.Json; +using System; +using System.Collections.Generic; +using System.Text; + +namespace CouchBot.UserCommands.Models +{ + public class CryptoData + { + [JsonProperty("bitcoin")] + public BitcoinData Bitcoin { get; set; } + + public class BitcoinData + { + [JsonProperty("usd")] + public double Usd { get; set; } + + [JsonProperty("usd_24h_change")] + public double Usd24hChange { get; set; } + } + } +} diff --git a/Modules/UserCommands.cs b/Modules/UserCommands.cs index c631f06..76b21a7 100644 --- a/Modules/UserCommands.cs +++ b/Modules/UserCommands.cs @@ -34,7 +34,6 @@ public class UserCommands : ModuleBase public async Task GetCovidDataAsync() { string output; - try { using var client = new HttpClient { BaseAddress = new Uri("https://corona.lmao.ninja/") }; @@ -68,27 +67,34 @@ public async Task GetCovidDataAsync() await ReplyAsync(output); } - [Command("Advice", RunMode = RunMode.Async)] - [Alias("advice", "quote", "tip")] - [Summary("returns a random life advice")] + [Command("bitcoin", RunMode = RunMode.Async)] + [Alias("bitcoin price", "bitcoin status", "bitcoin updates", "bitcoin update", "bitcoin")] + [Summary("returns the current price of bitcoin vs usd")] public async Task GetAdviceAsync() { - string errorMessage = "An error occured while fetching advice"; - + string errorMessage = "Unable to fetch current price of bitcoin. Try later!"; + string output; try { - using var client = new HttpClient { BaseAddress = new Uri("https://api.adviceslip.com/") }; - var response = await client.GetAsync("advice"); + using var client = new HttpClient { BaseAddress = new Uri("https://api.coingecko.com/") }; + var response = await client.GetAsync("api/v3/simple/price?ids=bitcoin&vs_currencies=usd&include_market_cap=false&include_24hr_vol=false&include_24hr_change=true&include_last_updated_at=false"); if (response.IsSuccessStatusCode) { var data = response.Content.ReadAsStringAsync().GetAwaiter().GetResult(); - var advice = JsonConvert.DeserializeObject(data); - await ReplyAsync(advice.AdviceSlip.AdviceText); + var btcData = (JsonConvert.DeserializeObject(data)); + var number = (int)btcData.Bitcoin.Usd24hChange; + + output = number switch + { + 0 => $"Just flat and constant! Bitcoin is {btcData.Bitcoin.Usd:n0} per USD with almost {btcData.Bitcoin.Usd24hChange:n0} percent change reported in last 24 hours.", + 1 => $"Nothing much interesting in crypto market today! Currently bitcoin is {btcData.Bitcoin.Usd:n0} per USD, with just {btcData.Bitcoin.Usd24hChange:n0} percent change reported in last 24 hours.", + _ => $"Breaking News! There is a {btcData.Bitcoin.Usd24hChange:n0} percent change in price of bitcoin, currently standing at {btcData.Bitcoin.Usd:n0} per USD, " + }; } else { - await ReplyAsync(errorMessage); + output = errorMessage; } } catch (Exception) @@ -96,5 +102,13 @@ public async Task GetAdviceAsync() await ReplyAsync(errorMessage); } } + + [Command("commandName", RunMode = RunMode.Async)] + [Alias("alias1", "alias2", "etc")] + [Summary("Description of the command")] + public Task CommandAsync() + { + return ReplyAsync("Your Return Text!"); + } } } \ No newline at end of file From 457b49a1b560393060db8f6c447ec1496d2355c5 Mon Sep 17 00:00:00 2001 From: pratikthorat Date: Wed, 21 Oct 2020 17:17:02 +0530 Subject: [PATCH 2/2] Added user command for receiving bitcoin updates. --- Modules/UserCommands.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Modules/UserCommands.cs b/Modules/UserCommands.cs index 76b21a7..d846b4f 100644 --- a/Modules/UserCommands.cs +++ b/Modules/UserCommands.cs @@ -99,8 +99,9 @@ public async Task GetAdviceAsync() } catch (Exception) { - await ReplyAsync(errorMessage); + output = errorMessage; } + await ReplyAsync(output); } [Command("commandName", RunMode = RunMode.Async)]