updated EXAMPLES.md for Program_Heroku

This commit is contained in:
Wizou 2022-03-28 13:24:37 +02:00
parent 74f22a2f5c
commit 63faf9a070
2 changed files with 24 additions and 3 deletions

View file

@ -404,3 +404,23 @@ var messages = await client.Messages_Search<InputMessagesFilterPinned>(chat, lim
foreach (var msg in messages.Messages) foreach (var msg in messages.Messages)
await client.Messages_SendReaction(chat, msg.ID, reaction); await client.Messages_SendReaction(chat, msg.ID, reaction);
``` ```
<a name="heroku"></a><a name="vps"></a><a name="host"></a>
### 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.
<a name="database"></a><a name="sessionStore"></a><a name="customStore"></a>
### 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)

View file

@ -25,7 +25,8 @@ namespace WTelegramClientTest
{ {
var exit = new SemaphoreSlim(0); var exit = new SemaphoreSlim(0);
AppDomain.CurrentDomain.ProcessExit += (s, e) => exit.Release(); // detect SIGTERM to exit gracefully 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); Client = new WTelegram.Client(store.Length == 0 ? null : Environment.GetEnvironmentVariable, store);
using (Client) using (Client)
{ {
@ -65,9 +66,9 @@ namespace WTelegramClientTest
private DateTime _lastWrite; private DateTime _lastWrite;
private Task _delayedWrite; 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 = 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(); _sql.Open();
using (var create = new NpgsqlCommand($"CREATE TABLE IF NOT EXISTS WTelegram (name text NOT NULL PRIMARY KEY, data bytea)", _sql)) using (var create = new NpgsqlCommand($"CREATE TABLE IF NOT EXISTS WTelegram (name text NOT NULL PRIMARY KEY, data bytea)", _sql))