Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
bjoern committed May 21, 2018
1 parent 2a2f12a commit 7d93417
Show file tree
Hide file tree
Showing 9 changed files with 476 additions and 0 deletions.
25 changes: 25 additions & 0 deletions Emby.Plugin.TelegramNotification.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.27703.2000
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Emby.Plugin.TelegramNotification", "Emby.Plugin.TelegramNotification\Emby.Plugin.TelegramNotification.csproj", "{0A206AE4-C2EE-4D5A-930F-03BD9B48AE28}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{0A206AE4-C2EE-4D5A-930F-03BD9B48AE28}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0A206AE4-C2EE-4D5A-930F-03BD9B48AE28}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0A206AE4-C2EE-4D5A-930F-03BD9B48AE28}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0A206AE4-C2EE-4D5A-930F-03BD9B48AE28}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {FF29D56C-B55D-49AB-B561-2E3E356CFC0D}
EndGlobalSection
EndGlobal
65 changes: 65 additions & 0 deletions Emby.Plugin.TelegramNotification/Api/ServerApiEntryPoints.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using MediaBrowser.Common.Net;
using MediaBrowser.Controller.Net;
using MediaBrowser.Model.Logging;
using MediaBrowser.Model.Services;
using Emby.Plugin.TelegramNotification.Configuration;

namespace Emby.Plugin.TelegramNotification.Api
{
[Route("/Notification/Telegram/Test/{UserID}", "POST", Summary = "Tests Telegram")]
public class TestNotification : IReturnVoid
{
[ApiMember(Name = "UserID", Description = "User Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
public string UserID { get; set; }
}

class ServerApiEndpoints : IService
{
private readonly IHttpClient _httpClient;
private readonly ILogger _logger;

public ServerApiEndpoints(ILogManager logManager, IHttpClient httpClient)
{
_logger = logManager.GetLogger(GetType().Name);
_httpClient = httpClient;
}
private TeleGramOptions GetOptions(String userID)
{
return Plugin.Instance.Configuration.Options
.FirstOrDefault(i => string.Equals(i.MediaBrowserUserId, userID, StringComparison.OrdinalIgnoreCase));
}

public void Post(TestNotification request)
{
var task = PostAsync(request);
Task.WaitAll(task);
}

public async Task PostAsync(TestNotification request)
{
var options = GetOptions(request.UserID);
string message = "This is a Test";

_logger.Debug("Telegram <TEST> to {0} - {1}", options.BotToken, options.ChatID);

var httpRequestOptions = new HttpRequestOptions
{
Url = "https://api.telegram.org/bot" + options.BotToken + "/sendmessage?chat_id=" + options.ChatID + "&text=" + message,
CancellationToken = CancellationToken.None
};


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

}


}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System;
using System.Collections.Generic;
using MediaBrowser.Model.Plugins;

namespace Emby.Plugin.TelegramNotification.Configuration
{
/// <summary>
/// Class PluginConfiguration
/// </summary>
public class PluginConfiguration : BasePluginConfiguration
{
public TeleGramOptions[] Options { get; set; }

public PluginConfiguration()
{
Options = new TeleGramOptions[] { };
}
}

public class TeleGramOptions
{
public Boolean Enabled { get; set; }
public String ChatID { get; set; }
public String BotToken { get; set; }
public String DeviceName { get; set; }
public List<Sound> SoundList { get; set; }
public int Priority { get; set; }
public string MediaBrowserUserId { get; set; }

public TeleGramOptions()
{
SoundList = new List<Sound>
{
new Sound() {Name = "Telegram", Value = "telegram"},
new Sound() {Name = "Bike", Value = "bike"},
new Sound() {Name = "Bugle", Value = "bugle"}
};
}
}

public class Sound
{
public String Name { get; set; }
public String Value { get; set; }
}
}
174 changes: 174 additions & 0 deletions Emby.Plugin.TelegramNotification/Configuration/config.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
<!DOCTYPE html>
<html>
<head>
<title>Telegram Notifications</title>
</head>
<body>
<div data-role="page" class="page type-interior pluginConfigurationPage teleGramConfigurationPage" data-require="emby-input,emby-button,emby-checkbox,emby-select">

<div data-role="content">
<div class="content-primary">
<form class="teleGramConfigurationForm">
<p>To use Telegram create a bot at @BotFather</p>

<div style="height:0; overflow: hidden;"><input type="text" name="fakeusernameremembered" tabindex="-1" /><input type="password" name="fakepasswordremembered" tabindex="-1" /></div>

<div class="selectContainer">
<select is="emby-select" id="selectUser" label="Configure Telegram notifications for:"></select>
</div>
<label class="checkboxContainer">
<input is="emby-checkbox" type="checkbox" id="chkEnableTeleGram" />
<span>Enabled</span>
</label>
<div class="inputContainer">
<input is="emby-input" type="text" id="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" id="txtTeleGramChatID" label="Telegram user key:" required="required" />
<div class="fieldDescription">
Telegram chat ID, get it from @get_id_bot
</div>
</div>
<div>
<button is="emby-button" type="button" class="raised button-cancel block" id="testNotification">
<span>Test Notification</span>
</button>
</div>
<br />
<div>
<button is="emby-button" type="submit" class="raised button-submit block"><span>Save</span></button>
</div>

</form>
</div>
</div>

<script type="text/javascript">

(function () {

var pluginId = "890ACB04-34A2-4CDB-8D89-4EA2FE90B0D7";

function loadUserConfig(page, userId) {

Dashboard.showLoadingMsg();

ApiClient.getPluginConfiguration(pluginId).then(function (config) {

var teleGramConfig = config.Options.filter(function (c) {

return userId == c.MediaBrowserUserId;

})[0] || {};

$('#chkEnableTeleGram', page).checked(teleGramConfig.Enabled || false).checkboxradio("refresh");
$('#txtTeleGramBotKey', page).val(teleGramConfig.BotToken || '');
$('#txtTeleGramChatID', page).val(teleGramConfig.ChatID || '');

Dashboard.hideLoadingMsg();
});
}

$('.teleGramConfigurationPage').on('pageinit', function (event) {

var page = this;

$('#selectUser', page).on('change', function () {

loadUserConfig(page, this.value);
});

$('#testNotification', page).on('click', function (event) {

Dashboard.showLoadingMsg();

var onError = function () {
alert("There was an error sending the test notification. Please check your notification settings and try again.");
require(['alert'], function (alert) {
Dashboard.hideLoadingMsg();
});
};

ApiClient.getPluginConfiguration(pluginId).then(function (config) {

if (!config.Options.length) {
Dashboard.hideLoadingMsg();
require(['alert'], function (alert) {
alert("Please configure and save at least one notification account.");
});
}

config.Options.map(function (c) {

ApiClient.ajax({
type: "POST",
url: ApiClient.getUrl("Notification/Telegram/Test/" + c.MediaBrowserUserId)

}).then(function () {
Dashboard.hideLoadingMsg();
}, onError);
});
});
});

}).on('pageshow', function (event) {

Dashboard.showLoadingMsg();

var page = this;

ApiClient.getUsers().then(function (users) {

$('#selectUser', page).html(users.map(function (user) {

return '<option value="' + user.Id + '">' + user.Name + '</option>';

})).selectmenu('refresh').trigger('change');

});

Dashboard.hideLoadingMsg();
});

$('.teleGramConfigurationForm').on('submit', function (e) {

Dashboard.showLoadingMsg();

var form = this;

ApiClient.getPluginConfiguration(pluginId).then(function (config) {

var userId = $('#selectUser', form).val();

var teleGramConfig = config.Options.filter(function (c) {

return userId == c.MediaBrowserUserId;

})[0];

if (!teleGramConfig) {
teleGramConfig = {};
config.Options.push(teleGramConfig);
}

teleGramConfig.MediaBrowserUserId = userId;

teleGramConfig.Enabled = $('#chkEnableTeleGram', form).checked();
teleGramConfig.BotToken = $('#txtTeleGramBotKey', form).val();
teleGramConfig.ChatID = $('#txtTeleGramChatID', form).val();

ApiClient.updatePluginConfiguration(pluginId, config).then(Dashboard.processPluginConfigurationUpdateResult);
});

return false;
});

})();

</script>
</div>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup> <TargetFrameworks>netstandard1.3;</TargetFrameworks> <AssemblyVersion>1.0.0.0</AssemblyVersion> <FileVersion>1.0.0.0</FileVersion> </PropertyGroup>

<ItemGroup>
<None Remove="Configuration\config.html" />
</ItemGroup>

<ItemGroup>
<EmbeddedResource Include="Configuration\config.html" />
</ItemGroup>

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

</Project>
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 7d93417

Please sign in to comment.