initial commit

This commit is contained in:
zotan 2018-02-07 16:28:49 +01:00
commit 4b4e33865e
No known key found for this signature in database
GPG key ID: A8DA209231E36475
3 changed files with 225 additions and 0 deletions

200
Program.cs Normal file
View file

@ -0,0 +1,200 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Telegram.Bot;
using Telegram.Bot.Args;
using Telegram.Bot.Types;
using Telegram.Bot.Types.Enums;
using Telegram.Bot.Types.InlineQueryResults;
using Telegram.Bot.Types.ReplyMarkups;
namespace TelegramRemindMe
{
public static class Program
{
//TODO: multiuser uwu
private static bool isAddingTask = false;
private static List<string> tasks = new List<string>();
private static readonly TelegramBotClient Bot = new TelegramBotClient("511835815:AAHewCgiXep58wF6tDWIW133LhfqjCbb8c4");
public static void Main(string[] args)
{
Bot.OnMessage += BotOnMessageReceived;
Bot.OnMessageEdited += BotOnMessageReceived;
Bot.OnCallbackQuery += BotOnCallbackQueryReceived;
Bot.OnInlineQuery += BotOnInlineQueryReceived;
Bot.OnInlineResultChosen += BotOnChosenInlineResultReceived;
Bot.OnReceiveError += BotOnReceiveError;
var me = Bot.GetMeAsync().Result;
Console.Title = me.Username;
Bot.StartReceiving();
Console.WriteLine($"Start listening for @{me.Username}");
Console.ReadLine();
Bot.StopReceiving();
}
private static async void BotOnMessageReceived(object sender, MessageEventArgs messageEventArgs)
{
var message = messageEventArgs.Message;
if (message == null || message.Type != MessageType.Text) return;
//IReplyMarkup keyboard = new ReplyKeyboardRemove();
var actionKeyboard = new InlineKeyboardMarkup(new[]
{
new []
{
InlineKeyboardButton.WithCallbackData("Add a new task"),
InlineKeyboardButton.WithCallbackData("List tasks"),
},
new []
{
InlineKeyboardButton.WithCallbackData("Add a new reminder"),
InlineKeyboardButton.WithCallbackData("List reminders"),
}
});
switch (message.Text.Split(' ').First())
{
case "/start":
case "/do":
case "/actions":
case "/help":
await Bot.SendChatActionAsync(message.Chat.Id, ChatAction.Typing);
//await Task.Delay(500);
await Bot.SendTextMessageAsync(
message.Chat.Id,
"What do you want me to do?",
replyMarkup: actionKeyboard);
break;
case "/echo":
await Bot.SendChatActionAsync(message.Chat.Id, ChatAction.RecordVideoNote);
await Task.Delay(500);
await Bot.SendChatActionAsync(message.Chat.Id, ChatAction.UploadDocument);
await Task.Delay(500);
await Bot.SendChatActionAsync(message.Chat.Id, ChatAction.RecordAudio);
await Task.Delay(500);
await Bot.SendChatActionAsync(message.Chat.Id, ChatAction.Typing);
await Task.Delay(500);
await Bot.SendTextMessageAsync(
message.Chat.Id,
"MEOW" + message.Text.Substring(5));
break;
default:
if (isAddingTask)
{
tasks.Add(message.Text);
await Bot.SendTextMessageAsync(
message.Chat.Id,
"Added. Anything else?",
replyMarkup: actionKeyboard);
}
break;
}
}
private static async void BotOnCallbackQueryReceived(object sender, CallbackQueryEventArgs callbackQueryEventArgs)
{
Console.WriteLine($"Received action request: {callbackQueryEventArgs.CallbackQuery.Data}");
switch (callbackQueryEventArgs.CallbackQuery.Data)
{
case "Add a new task":
await Bot.EditMessageTextAsync(
new ChatId(callbackQueryEventArgs.CallbackQuery.Message.Chat.Id),
callbackQueryEventArgs.CallbackQuery.Message.MessageId,
"Which task should I add?"
);
isAddingTask = true;
break;
case "List tasks":
var sb = new StringBuilder();
sb.AppendLine("Your tasks:");
foreach (var task in tasks)
{
sb.Append("[ ] ");
sb.AppendLine(task);
}
await Bot.EditMessageTextAsync(
new ChatId(callbackQueryEventArgs.CallbackQuery.Message.Chat.Id),
callbackQueryEventArgs.CallbackQuery.Message.MessageId,
sb.ToString()
);
break;
default:
Console.WriteLine("Wat (Unknown callback action)");
await Bot.AnswerCallbackQueryAsync(
callbackQueryEventArgs.CallbackQuery.Id,
"Wat");
break;
}
//await Bot.AnswerCallbackQueryAsync(
// callbackQueryEventArgs.CallbackQuery.Id,
// $"Received {callbackQueryEventArgs.CallbackQuery.Data}");
}
private static async void BotOnInlineQueryReceived(object sender, InlineQueryEventArgs inlineQueryEventArgs)
{
Console.WriteLine($"Received inline query from: {inlineQueryEventArgs.InlineQuery.From.Id}");
InlineQueryResultBase[] results = {
new InlineQueryResultLocation
{
Id = "1",
Latitude = 40.7058316f, // displayed result
Longitude = -74.2581888f,
Title = "New York",
InputMessageContent = new InputLocationMessageContent // message if result is selected
{
Latitude = 40.7058316f,
Longitude = -74.2581888f,
}
},
new InlineQueryResultLocation
{
Id = "2",
Longitude = 52.507629f, // displayed result
Latitude = 13.1449577f,
Title = "Berlin",
InputMessageContent = new InputLocationMessageContent // message if result is selected
{
Longitude = 52.507629f,
Latitude = 13.1449577f
}
}
};
await Bot.AnswerInlineQueryAsync(
inlineQueryEventArgs.InlineQuery.Id,
results,
isPersonal: true,
cacheTime: 0);
}
private static void BotOnChosenInlineResultReceived(object sender, ChosenInlineResultEventArgs chosenInlineResultEventArgs)
{
Console.WriteLine($"Received inline result: {chosenInlineResultEventArgs.ChosenInlineResult.ResultId}");
}
private static void BotOnReceiveError(object sender, ReceiveErrorEventArgs receiveErrorEventArgs)
{
Console.WriteLine("Received error: {0} — {1}",
receiveErrorEventArgs.ApiRequestException.ErrorCode,
receiveErrorEventArgs.ApiRequestException.Message);
}
}
}

9
TelegramRemindMe.csproj Normal file
View file

@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Telegram.Bot" Version="14.0.0-rc-367" />
</ItemGroup>
</Project>

16
TelegramRemindMe.sln Normal file
View file

@ -0,0 +1,16 @@

Microsoft Visual Studio Solution File, Format Version 12.00
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TelegramRemindMe", "TelegramRemindMe.csproj", "{58CB8963-3F46-4C77-A188-6E26FA4DA9FE}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{58CB8963-3F46-4C77-A188-6E26FA4DA9FE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{58CB8963-3F46-4C77-A188-6E26FA4DA9FE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{58CB8963-3F46-4C77-A188-6E26FA4DA9FE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{58CB8963-3F46-4C77-A188-6E26FA4DA9FE}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal