Add option to include article content in message

A configurable number of characters of article content can now be
included in sent messages.
Preferences layout is also adjusted a bit.
This commit is contained in:
Roliga 2019-03-26 16:54:18 +01:00
parent 1904e3084f
commit 5001b7516d
2 changed files with 28 additions and 1 deletions

View file

@ -2,6 +2,7 @@
class Discord_Webhook extends Plugin {
private $host;
private $discord_api_url = "https://discordapp.com/api/webhooks/";
private $default_content_length = 0;
function about() {
return array(1.0,
@ -66,6 +67,22 @@ class Discord_Webhook extends Plugin {
$payload = array();
$payload["content"] = $article["title"] . " " . $article["link"];
$content_length = $this->host->get($this, 'content_length');
if (!is_numeric($content_length))
$content_length = $this->default_content_length;
if ($content_length > 0) {
$content_stripped = preg_replace('#<br\s*/?>#i', "\n", $article["content"]);
$content_stripped = strip_tags($content_stripped);
$content_stripped = trim($content_stripped);
if (strlen($content_stripped) > $content_length) {
$payload["content"] .= "\n_" . substr($content_stripped, 0, $content_length) . "..._";
} else {
$payload["content"] .= "\n_" . $content_stripped . "_";
}
}
$sth = $this->pdo->prepare("SELECT title FROM ttrss_feeds WHERE id = ?");
$sth->execute([$article["feed"]["id"]]);
@ -92,6 +109,8 @@ class Discord_Webhook extends Plugin {
$replacements = array(
'{webhook_url}' => $this->host->get($this, 'webhook_url'),
'{content_length}' => $this->host->get($this, 'content_length'),
'{default_content_length}' => $this->default_content_length,
'{discord_api_url}' => $this->discord_api_url
);
@ -103,6 +122,7 @@ class Discord_Webhook extends Plugin {
function save()
{
$this->host->set($this, 'webhook_url', $_POST['webhook_url']);
$this->host->set($this, 'content_length', $_POST['content_length']);
echo __("Configuration saved");
}

View file

@ -24,7 +24,14 @@
<input dojoType="dijit.form.TextBox" type="hidden" name="op" value="pluginhandler">
<input dojoType="dijit.form.TextBox" type="hidden" name="method" value="save">
<input dojoType="dijit.form.TextBox" type="hidden" name="plugin" value="discord_webhook">
<input dojoType="dijit.form.ValidationTextBox" id="discord_webhook_url" type="url" required="1" pattern="{discord_api_url}.+" title="{discord_api_url}..." placeholder="{discord_api_url}..." name="webhook_url" value="{webhook_url}">
<fieldset class="prefs">
<label>Webhook URL:</label>
<input dojoType="dijit.form.ValidationTextBox" id="discord_webhook_url" type="url" required="1" pattern="{discord_api_url}.+" placeholder="{discord_api_url}..." name="webhook_url" value="{webhook_url}">
</fieldset>
<fieldset class="prefs">
<label>Include characters of article content:</label>
<input dojoType="dijit.form.NumberSpinner" constraints="{min:0}" id="discord_content_length" placeholder="{default_content_length}" name="content_length" value="{content_length}">
</fieldset>
<br><br>
<button dojoType="dijit.form.Button" type="submit">Save</button>
</form>