diff --git a/streamerbot/1.get-started/5.examples/http-post.md b/streamerbot/1.get-started/5.examples/http-post.md index d046d0d0..2bf15c50 100644 --- a/streamerbot/1.get-started/5.examples/http-post.md +++ b/streamerbot/1.get-started/5.examples/http-post.md @@ -33,163 +33,163 @@ Read more about the `Execute C# Code` sub-action 1. Setup the HTTPClient - ```cs [Basic Setup] - using System; - using System.Text; - using System.Threading.Tasks; - using System.Net.Http; - using System.Net.Http.Headers; - using Newtonsoft.Json; - using Newtonsoft.Json.Linq; - - public class CPHInline { - // You can set the timeout to any length you need - private static readonly HttpClient _httpClient = new HttpClient{Timeout = TimeSpan.FromSeconds(30)}; - - public void Init() { - // Ensure we are working with a clean slate - _httpClient.DefaultRequestHeaders.Clear(); - } - - public void Dispose() { - // Free up allocations - _httpClient.Dispose(); - } + ```cs [Basic Setup] + using System; + using System.Text; + using System.Threading.Tasks; + using System.Net.Http; + using System.Net.Http.Headers; + using Newtonsoft.Json; + using Newtonsoft.Json.Linq; + + public class CPHInline { + // You can set the timeout to any length you need + private static readonly HttpClient _httpClient = new HttpClient{Timeout = TimeSpan.FromSeconds(30)}; + + public void Init() { + // Ensure we are working with a clean slate + _httpClient.DefaultRequestHeaders.Clear(); + } - public bool Execute() { - // ... - return true; + public void Dispose() { + // Free up allocations + _httpClient.Dispose(); + } + + public bool Execute() { + // ... + return true; + } } - } - ``` + ``` - ::tip{color=amber} - While people using the HTTPClient in a `using` statement or creating a new one within the executed function may be a common occurence, it is heavily discouraged to do so. [Read More](https://extensions.streamer.bot/t/httpclient-and-you/1369) - :: + ::tip{color=amber} + While people using the HTTPClient in a `using` statement or creating a new one within the executed function may be a common occurence, it is heavily discouraged to do so. [Read More](https://extensions.streamer.bot/t/httpclient-and-you/1369) + :: 2. Setup the sending of your payload - We are going to send a `%userName%` and `%userId%` pair (from common triggers) as `PUT` request to our fictional logging website. - - ```cs [Send PUT payload in Execute method] - public bool Execute() { - // Get the arguments - or abort if it does not exist - if(!CPH.TryGetArg("userName", out string userName)) return false; - if(!CPH.TryGetArg("userId", out string userId)) return false; - - // Build a wrapper object as payload - var data = new { - id = userId, - name = userName - }; - - // Serialize JSON - string json = JsonConvert.SerializeObject(data); + We are going to send a `%userName%` and `%userId%` pair (from common triggers) as `PUT` request to our fictional logging website. - // Create web request payload - var payload = new StringContent(json, Encoding.UTF8, "application/json"); - - // Finally, send request - HttpResponseMessage response = _httpClient.PutAsync("https://my-logging-server.com", payload).GetAwaiter().GetResult(); - - // Do with the response what you need to. - // ... - - return true; - } - ``` + ```cs [Send PUT payload in Execute method] + public bool Execute() { + // Get the arguments - or abort if it does not exist + if(!CPH.TryGetArg("userName", out string userName)) return false; + if(!CPH.TryGetArg("userId", out string userId)) return false; + + // Build a wrapper object as payload + var data = new { + id = userId, + name = userName + }; + + // Serialize JSON + string json = JsonConvert.SerializeObject(data); + + // Create web request payload + var payload = new StringContent(json, Encoding.UTF8, "application/json"); + + // Finally, send request + HttpResponseMessage response = _httpClient.PutAsync("https://my-logging-server.com", payload).GetAwaiter().GetResult(); + + // Do with the response what you need to. + // ... + + return true; + } + ``` - - To use different request types, simply use the methods `GetAsync()`, `PutAsync()`, `PostAsync()` or `DeleteAsync()`. + To use different request types, simply use the methods `GetAsync()`, `PutAsync()`, `PostAsync()` or `DeleteAsync()`. 3. The PATCH request - Specifically the built-in handling for the `PATCH` request type may not be available in the .net versions used by StreamerBot. In this case, you need a workaround. + Specifically the built-in handling for the `PATCH` request type may not be available in the .net versions used by StreamerBot. In this case, you need a workaround. - ```cs [PATCH workaround] - // ... + ```cs [PATCH workaround] + // ... - // Finally, send the request - var request = new HttpRequestMessage(new HttpMethod("PATCH"), "https://my-logging-server.com"){ - Content = payload - }; - HttpResponseMessage response = _httpClient.SendAsync(request).GetAwaiter().GetResult(); + // Finally, send the request + var request = new HttpRequestMessage(new HttpMethod("PATCH"), "https://my-logging-server.com"){ + Content = payload + }; + HttpResponseMessage response = _httpClient.SendAsync(request).GetAwaiter().GetResult(); - // ... - ``` + // ... + ``` 4. Additional Headers - If you need to additionally include specific headers - like submitting an authorization token for the Twitch API or Discord API - make sure to clear and set the headers on each call you make. + If you need to additionally include specific headers - like submitting an authorization token for the Twitch API or Discord API - make sure to clear and set the headers on each call you make. - You can also send individual headers with each request itself, but that requires you to always use the `SendAsync` method and building your own `HttpRequestMessage`. + You can also send individual headers with each request itself, but that requires you to always use the `SendAsync` method and building your own `HttpRequestMessage`. - If your code only does a static request whose headers never change, resetting the headers is **not strictly necessary**. But it's good practice to not forget later when you need it. + If your code only does a static request whose headers never change, resetting the headers is **not strictly necessary**. But it's good practice to not forget later when you need it. - ```cs [Header management] - public bool Execute() { - // Making a call to the Twitch API to get all currently live streams - string token = CPH.TwitchOAuthToken; - string clientId = CPH.TwitchClientId; + ```cs [Header management] + public bool Execute() { + // Making a call to the Twitch API to get all currently live streams + string token = CPH.TwitchOAuthToken; + string clientId = CPH.TwitchClientId; - // Clear our old headers - _httpClient.DefaultRequestHeaders.Clear(); + // Clear our old headers + _httpClient.DefaultRequestHeaders.Clear(); - // Now set your Twitch API authorization - _httpClient.DefaultRequestHeaders.Add("Authorization", "Bearer "+token); - _httpClient.DefaultRequestHeaders.Add("Client-Id", clientId); + // Now set your Twitch API authorization + _httpClient.DefaultRequestHeaders.Add("Authorization", "Bearer "+token); + _httpClient.DefaultRequestHeaders.Add("Client-Id", clientId); - // Send your request - HttpResponseMessage response = _httpClient.GetAsync("https://api.twitch.tv/helix/streams").GetAwaiter().GetResult(); + // Send your request + HttpResponseMessage response = _httpClient.GetAsync("https://api.twitch.tv/helix/streams").GetAwaiter().GetResult(); - // Do with the response what you need to. - // ... + // Do with the response what you need to. + // ... - return true; - } - ``` + return true; + } + ``` 5. Handling the response - Since you have to do the entire request handling yourself, that includes handling the response. + Since you have to do the entire request handling yourself, that includes handling the response. - - If you don't need to know what the server responded, you can ignore it like in the examples above. + - If you don't need to know what the server responded, you can ignore it like in the examples above. - - If you need to know if the request succeeded or are expecting data in return, follow the below example. + - If you need to know if the request succeeded or are expecting data in return, follow the below example. - ```cs [Response handling] - public bool Execute() { - // ...all your data preparation + ```cs [Response handling] + public bool Execute() { + // ...all your data preparation - HttpResponseMessage response = _httpClient.GetAsync("https://api.twitch.tv/helix/streams").GetAwaiter().GetResult(); + HttpResponseMessage response = _httpClient.GetAsync("https://api.twitch.tv/helix/streams").GetAwaiter().GetResult(); - // Check if our request was successful - try { - if(!response.IsSuccessStatusCode) { - // It was not. Abort. - CPH.LogError($"{response.StatusCode} - {response.ReasonPhrase}"); - return false; - } + // Check if our request was successful + try { + if(!response.IsSuccessStatusCode) { + // It was not. Abort. + CPH.LogError($"{response.StatusCode} - {response.ReasonPhrase}"); + return false; + } - // Get the response data - string content = response.Content.ReadAsStringAsync().GetAwaiter().GetResult(); + // Get the response data + string content = response.Content.ReadAsStringAsync().GetAwaiter().GetResult(); - // Usually, data is JSON. So you would decode it now. - JObject parsed = JObject.Parse(content); + // Usually, data is JSON. So you would decode it now. + JObject parsed = JObject.Parse(content); - // And then do with the data whatever you need. - // ... + // And then do with the data whatever you need. + // ... - return true; - } catch (Exception e) { - // Something went wrong - CPH.LogError(e.Message); - return false; + return true; + } catch (Exception e) { + // Something went wrong + CPH.LogError(e.Message); + return false; + } } - } - ``` + ``` ## Tips & Tricks -Wrapping sensitive parts of code, especially when using web requests, into `try` `catch` blocks is good practice and prevents potential unexpected bigger issues you do not anticipate. + Wrapping sensitive parts of code, especially when using web requests, into `try` `catch` blocks is good practice and prevents potential unexpected bigger issues you do not anticipate. -Of course, proper error handling is always better, but not always necessary. \ No newline at end of file + Of course, proper error handling is always better, but not always necessary. \ No newline at end of file diff --git a/streamerbot/1.get-started/_faq/31.voicemod-v3.md b/streamerbot/1.get-started/_faq/31.voicemod-v3.md index 4405b24a..41f8ec09 100644 --- a/streamerbot/1.get-started/_faq/31.voicemod-v3.md +++ b/streamerbot/1.get-started/_faq/31.voicemod-v3.md @@ -2,10 +2,12 @@ description: Voicemod 3.0 is not connecting to Streamer.bot --- -Currently Voicemod 3.0 does not have an API which allows third-party applications, like Streamer.bot, to interact with it. +Streamer.bot versions **below 1.0.0** are **only compatible** with Voicemod version **2.51**. -## What do I do now that I installed v3 of Voicemod? -You can download v2.51 of Voicemod by going to their website and get to the v3 download, which automatically starts. Stop the download and you'll see `Are you looking for v2 version? you can download here.` which will lead you to the v.2.51 which can connect to Streamer.bot. +Streamer.bot **v1.0.0 and above require** Voicemod **v3.12 or higher**. Earlier Voicemod versions will not work with SB 1.0.0+ + +Note: Streamer.bot v1.0.0 is currently in alpha. You can gain alpha access by becoming a *Bronze Tier* supporter (or higher) on [Patreon](https://www.patreon.com/nate1280/)! -## Do you know when v3 will be able to connect to Streamer.bot -That is mostly up to Voicemod itself as they are working on adapting v3 to be able to work with every application that was working with v2. For more information you'd want to contact Voicemod. +## What if I already installed Voicemod v3? +If you're using a version of Streamer.bot below 1.0.0, you'll need to downgrade Voicemod to v2.51 for compatibility. +You can download v2.51 of Voicemod by going to their website and get to the v3 download, which automatically starts. Stop the download and you'll see `Are you looking for v2 version? you can download here.` which will lead you to the v.2.51 which can connect to Streamer.bot.