From 63faf9a0708ec5a2789d9417c28b284ed0d1384f Mon Sep 17 00:00:00 2001 From: Wizou <11647984+wiz0u@users.noreply.github.com> Date: Mon, 28 Mar 2022 13:24:37 +0200 Subject: [PATCH] updated EXAMPLES.md for Program_Heroku --- EXAMPLES.md | 20 ++++++++++++++++++++ Examples/Program_Heroku.cs | 7 ++++--- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/EXAMPLES.md b/EXAMPLES.md index e4f5e71..ac80784 100644 --- a/EXAMPLES.md +++ b/EXAMPLES.md @@ -404,3 +404,23 @@ var messages = await client.Messages_Search(chat, lim foreach (var msg in messages.Messages) await client.Messages_SendReaction(chat, msg.ID, reaction); ``` + +### How to host my userbot online? + +If you need your userbot to run 24/7, you would typically design your userbot as a Console program, compiled for Linux or Windows, +and hosted online on any [VPS Hosting](https://www.google.com/search?q=vps+hosting) (Virtual Private Server). +Pure WebApp hosts might not be adequate as they will recycle (stop) your app if there is no incoming HTTP requests. + +There are many cheap VPS Hosting offers available, and some even have free tier, like Heroku: +See [Examples/Program_Heroku.cs](Examples/Program_Heroku.cs) for such an implementation and the steps to host/deploy it. + + +### Store session data to database or elsewhere, instead of files + +If you don't want to store session data into files *(for example if your VPS Hosting doesn't allow that)*, or just for easier management, +you can choose to store the session data somewhere else, like in a database. + +The WTelegram.Client constructor takes an optional `sessionStore` parameter to allow storing sessions in a custom manner. +Use it to pass a custom Stream-derived class that will **read** (first initial call to Length & Read) and **store** (subsequent Writes) session data to database. + +You can find an example for such custom session store in [Examples/Program_Heroku.cs](https://github.com/wiz0u/WTelegramClient/blob/master/Examples/Program_Heroku.cs#L61) diff --git a/Examples/Program_Heroku.cs b/Examples/Program_Heroku.cs index df4dc61..a819843 100644 --- a/Examples/Program_Heroku.cs +++ b/Examples/Program_Heroku.cs @@ -25,7 +25,8 @@ namespace WTelegramClientTest { var exit = new SemaphoreSlim(0); AppDomain.CurrentDomain.ProcessExit += (s, e) => exit.Release(); // detect SIGTERM to exit gracefully - var store = new PostgreStore(); // if DB does not contain a session, client will be run in interactive mode + var store = new PostgreStore(Environment.GetEnvironmentVariable("DATABASE_URL")); + // if DB does not contain a session yet, client will be run in interactive mode Client = new WTelegram.Client(store.Length == 0 ? null : Environment.GetEnvironmentVariable, store); using (Client) { @@ -65,9 +66,9 @@ namespace WTelegramClientTest private DateTime _lastWrite; private Task _delayedWrite; - public PostgreStore() + public PostgreStore(string databaseUrl) // Heroku DB URL of the form "postgres://user:password@host:port/database" { - var parts = Environment.GetEnvironmentVariable("DATABASE_URL").Split(':', '/', '@'); // parse Heroku DB URL + var parts = databaseUrl.Split(':', '/', '@'); _sql = new NpgsqlConnection($"User ID={parts[3]};Password={parts[4]};Host={parts[5]};Port={parts[6]};Database={parts[7]};Pooling=true;SSL Mode=Require;Trust Server Certificate=True;"); _sql.Open(); using (var create = new NpgsqlCommand($"CREATE TABLE IF NOT EXISTS WTelegram (name text NOT NULL PRIMARY KEY, data bytea)", _sql))