Skip to content

Commit

Permalink
Merge pull request #9 from LukePulverenti/master
Browse files Browse the repository at this point in the history
4.8.0.20 compatibility update
  • Loading branch information
bjoerns1983 authored Dec 23, 2022
2 parents 4c5a7e4 + 0dfbbc1 commit 9942786
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 166 deletions.
66 changes: 0 additions & 66 deletions Emby.Plugin.TelegramNotification/Api/ServerApiEntryPoints.cs

This file was deleted.

This file was deleted.

37 changes: 37 additions & 0 deletions Emby.Plugin.TelegramNotification/Configuration/entryeditor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
define(['globalize', 'pluginManager', 'emby-input'], function (globalize, pluginManager) {
'use strict';

function EntryEditor() {
}

EntryEditor.setObjectValues = function (context, entry) {

entry.FriendlyName = context.querySelector('.txtFriendlyName').value;
entry.Options.Token = context.querySelector('.BotToken').value;
entry.Options.Token = context.querySelector('.ChatID').value;
};

EntryEditor.setFormValues = function (context, entry) {

context.querySelector('.txtFriendlyName').value = entry.FriendlyName || '';
context.querySelector('.BotToken').value = entry.Options.Token || '';
context.querySelector('.ChatID').value = entry.Options.Token || '';
};

EntryEditor.loadTemplate = function (context) {

return require(['text!' + pluginManager.getConfigurationResourceUrl('telegrameditortemplate')]).then(function (responses) {

var template = responses[0];
context.innerHTML = globalize.translateDocument(template);

// setup any required event handlers here
});
};

EntryEditor.destroy = function () {

};

return EntryEditor;
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<div class="inputContainer">
<input is="emby-input" type="text" class="txtFriendlyName" required="required" label="${LabelName}" />
<div class="fieldDescription">${FriendlyNameForReferenceHelp}</div>
</div>

<div class="inputContainer">
<input is="emby-input" type="text" class="txtTeleGramBotKey" label="Telegram bot token:" required="required" />
<div class="fieldDescription">
Bot token for your bot, get it from @BotFather
</div>
</div>
<div class="inputContainer">
<input is="emby-input" type="text" class="txtTeleGramChatID" label="Telegram ChatID key:" required="required" />
<div class="fieldDescription">
Telegram chat ID, get it from @get_id_bot
</div>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,26 @@
<FileVersion>1.2.0.0</FileVersion>
</PropertyGroup>

<ItemGroup>
<Compile Remove="Images\**" />
<EmbeddedResource Remove="Images\**" />
<None Remove="Images\**" />
</ItemGroup>

<ItemGroup>
<None Remove="Configuration\config.html" />
<None Remove="Configuration\entryeditor.js" />
<None Remove="Configuration\entryeditor.template.html" />
<None Remove="thumb.png" />
</ItemGroup>

<ItemGroup>
<EmbeddedResource Include="Configuration\config.html" />
<EmbeddedResource Include="Configuration\entryeditor.js" />
<EmbeddedResource Include="Configuration\entryeditor.template.html" />
<EmbeddedResource Include="thumb.png" />
</ItemGroup>

<ItemGroup> <PackageReference Include="mediabrowser.server.core" Version="4.0.0.2" /> </ItemGroup>

<ItemGroup>
<Folder Include="Images\" />
</ItemGroup>
<ItemGroup> <PackageReference Include="mediabrowser.server.core" Version="4.8.0.20-beta12" /> </ItemGroup>

</Project>
64 changes: 25 additions & 39 deletions Emby.Plugin.TelegramNotification/Notifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,57 +3,50 @@
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Notifications;
using MediaBrowser.Model.Logging;
using Emby.Plugin.TelegramNotification.Configuration;
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Emby.Notifications;
using MediaBrowser.Controller;

namespace Emby.Plugin.TelegramNotification
{
public class Notifier : INotificationService
public class Notifier : IUserNotifier
{
private readonly ILogger _logger;
private readonly IHttpClient _httpClient;
private ILogger _logger;
private IServerApplicationHost _appHost;
private IHttpClient _httpClient;

public Notifier(ILogManager logManager, IHttpClient httpClient)
public Notifier(ILogger logger, IServerApplicationHost applicationHost, IHttpClient httpClient)
{
_logger = logManager.GetLogger(GetType().Name);
_logger = logger;
_appHost = applicationHost;
_httpClient = httpClient;
}

public bool IsEnabledForUser(User user)
{
var options = GetOptions(user);
private Plugin Plugin => _appHost.Plugins.OfType<Plugin>().First();

return options != null && IsValid(options) && options.Enabled;
}
public string Name => Plugin.StaticName;

private TeleGramOptions GetOptions(User user)
{
return Plugin.Instance.Configuration.Options
.FirstOrDefault(i => string.Equals(i.MediaBrowserUserId, user.Id.ToString("N"), StringComparison.OrdinalIgnoreCase));
}
public string Key => "telegramnotifications";

public string Name
{
get { return Plugin.Instance.Name; }
}
public string SetupModuleUrl => Plugin.NotificationSetupModuleUrl;

public async Task SendNotification(UserNotification request, CancellationToken cancellationToken)
public async Task SendNotification(InternalNotificationRequest request, CancellationToken cancellationToken)
{
var options = request.Configuration.Options;

options.TryGetValue("BotToken", out string botToken);
options.TryGetValue("ChatID", out string chatID);

var options = GetOptions(request.User);
string message = (request.Name);
string message = (request.Title);

if (string.IsNullOrEmpty(request.Description) == false && options.SendDescription == true)
if (string.IsNullOrEmpty(request.Description) == false)
{
message = (request.Name + "\n\n" + request.Description);
message = (request.Title + "\n\n" + request.Description);
}

_logger.Debug("TeleGram to Token : {0} - {1} - {2}", options.BotToken, options.ChatID, request.Name);


if (message.Length > 4096)
{
int chunkSize = 4096;
Expand All @@ -64,8 +57,8 @@ public async Task SendNotification(UserNotification request, CancellationToken c
string TelegramMessage = Uri.EscapeDataString(message.Substring(i, chunkSize));
var httpRequestOptions = new HttpRequestOptions
{
Url = "https://api.telegram.org/bot" + options.BotToken + "/sendmessage?chat_id=" + options.ChatID + "&text=" + TelegramMessage,
CancellationToken = CancellationToken.None
Url = "https://api.telegram.org/bot" + botToken + "/sendmessage?chat_id=" + chatID + "&text=" + TelegramMessage,
CancellationToken = cancellationToken
};
using (await _httpClient.Post(httpRequestOptions).ConfigureAwait(false))
{
Expand All @@ -78,22 +71,15 @@ public async Task SendNotification(UserNotification request, CancellationToken c
string TelegramMessage = Uri.EscapeDataString(message);
var httpRequestOptions = new HttpRequestOptions
{
Url = "https://api.telegram.org/bot" + options.BotToken + "/sendmessage?chat_id=" + options.ChatID + "&text=" + TelegramMessage,
CancellationToken = CancellationToken.None
Url = "https://api.telegram.org/bot" + botToken + "/sendmessage?chat_id=" + chatID + "&text=" + TelegramMessage,
CancellationToken = cancellationToken
};

using (await _httpClient.Post(httpRequestOptions).ConfigureAwait(false))
{

}
}

}

private bool IsValid(TeleGramOptions options)
{
return !string.IsNullOrEmpty(options.ChatID) &&
!string.IsNullOrEmpty(options.BotToken);
}
}
}
43 changes: 21 additions & 22 deletions Emby.Plugin.TelegramNotification/Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using MediaBrowser.Common.Plugins;
using MediaBrowser.Model.Plugins;
using MediaBrowser.Model.Serialization;
using Emby.Plugin.TelegramNotification.Configuration;
using System.IO;
using MediaBrowser.Model.Drawing;

Expand All @@ -13,39 +12,45 @@ namespace Emby.Plugin.TelegramNotification
/// <summary>
/// Class Plugin
/// </summary>
public class Plugin : BasePlugin<PluginConfiguration>, IHasWebPages, IHasThumbImage
public class Plugin : BasePlugin, IHasWebPages, IHasThumbImage
{
public Plugin(IApplicationPaths applicationPaths, IXmlSerializer xmlSerializer)
: base(applicationPaths, xmlSerializer)
{
Instance = this;
}

private Guid _id = new Guid("890ACB04-34A2-4CDB-8D89-4EA2FE90B0D7");
public override Guid Id
{
get { return _id; }
}
private const string EditorJsName = "telegramnotificationeditorjs";

public IEnumerable<PluginPageInfo> GetPages()
{
return new[]
{
new PluginPageInfo
{
Name = Name,
EmbeddedResourcePath = GetType().Namespace + ".Configuration.config.html"
Name = EditorJsName,
EmbeddedResourcePath = GetType().Namespace + ".Configuration.entryeditor.js"
},
new PluginPageInfo
{
Name = "telegrameditortemplate",
EmbeddedResourcePath = GetType().Namespace + ".Configuration.entryeditor.template.html",
IsMainConfigPage = false
}
};
}

public string NotificationSetupModuleUrl => GetPluginPageUrl(EditorJsName);

private Guid _id = new Guid("890ACB04-34A2-4CDB-8D89-4EA2FE90B0D7");
public override Guid Id
{
get { return _id; }
}

public static string StaticName = "Telegram";

/// <summary>
/// Gets the name of the plugin
/// </summary>
/// <value>The name.</value>
public override string Name
{
get { return "Telegram Notifications"; }
get { return StaticName + " Notifications"; }
}

/// <summary>
Expand Down Expand Up @@ -73,11 +78,5 @@ public ImageFormat ThumbImageFormat
return ImageFormat.Png;
}
}

/// <summary>
/// Gets the instance.
/// </summary>
/// <value>The instance.</value>
public static Plugin Instance { get; private set; }
}
}

0 comments on commit 9942786

Please sign in to comment.