-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathYourBot.cs
71 lines (67 loc) · 2.9 KB
/
YourBot.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
using System;
using System.Threading.Tasks;
using Telegram.Bot;
using Telegram.Bot.Types;
using Telegram.Bot.Types.ReplyMarkups;
namespace YourEasyBot
{
/* Notes:
* 1. To use this example, first paste your bot token in the Project Properties > Debug > Launch profiles > Command line arguments
* 2. This software is provided "as-is", without any warranty or liability (license MIT)
* 3. This project can help you write little bots with sequential logic methods, awaiting consecutive user messages
* 3.14 However this was written as fun project, it would not be reasonable to use it in serious company code
*/
public class YourBot(string botToken) : EasyBot(botToken)
{
static void Main(string[] args)
{
var bot = new YourBot(args[0]);
bot.Run();
}
public override async Task OnPrivateChat(Chat chat, User user, UpdateInfo update)
{
if (update.UpdateKind != UpdateKind.NewMessage || update.MsgCategory != MsgCategory.Text)
return;
if (update.Message.Text == "/start")
{
await Telegram.SendMessage(chat, "What is your first name?");
var firstName = await NewTextMessage(update);
// execution continues here once we received a new text message
await Telegram.SendMessage(chat, "What is your last name?");
var lastName = await NewTextMessage(update);
var genderMsg = await Telegram.SendMessage(chat, "What is your gender?", replyMarkup: new InlineKeyboardMarkup(new InlineKeyboardButton[]
{
new("Male") { CallbackData = "🚹" }, new("Female") { CallbackData = "🚺" }, new("Other") { CallbackData = "⚧" }
}));
var genderEmoji = await ButtonClicked(update, genderMsg);
ReplyCallback(update, "You clicked " + genderEmoji);
await Telegram.SendMessage(chat, $"Welcome, {firstName} {lastName}! ({genderEmoji})" +
$"\n\nFor more fun, try to type /button@{BotName} in a group I'm in");
return;
}
}
public override async Task OnGroupChat(Chat chat, UpdateInfo update)
{
Console.WriteLine($"In group chat {chat.Name()}");
do
{
switch (update.UpdateKind)
{
case UpdateKind.NewMessage:
Console.WriteLine($"{update.Message.From.Name()} wrote: {update.Message.Text}");
if (update.Message.Text == "/button@" + BotName)
await Telegram.SendMessage(chat, "You summoned me!", replyMarkup: new InlineKeyboardMarkup("I grant your wish"));
break;
case UpdateKind.EditedMessage:
Console.WriteLine($"{update.Message.From.Name()} edited: {update.Message.Text}");
break;
case UpdateKind.CallbackQuery:
Console.WriteLine($"{update.Message.From.Name()} clicked the button with data '{update.CallbackData}' on the msg: {update.Message.Text}");
ReplyCallback(update, "Wish granted !", showAlert: true);
break;
}
// in this approach, we choose to continue execution in a loop, obtaining new updates/messages for this chat as they come
} while (await NextEvent(update) != 0);
}
}
}