Created
August 7, 2020 12:30
-
-
Save immmdreza/90127d64654fcac0e006138dfdfc4d7a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Concurrent; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Threading.Tasks; | |
using Telegram.Bot; | |
using Telegram.Bot.Types; | |
using Telegram.Bot.Types.Enums; | |
using Telegram.Bot.Types.ReplyMarkups; | |
namespace Tester | |
{ | |
class Program | |
{ | |
private static readonly ITelegramBotClient telegramBotClient = | |
new TelegramBotClient("botToken"); | |
private static readonly ConcurrentDictionary<int, ConversationState> state = | |
new ConcurrentDictionary<int, ConversationState>(); | |
private static readonly ConcurrentDictionary<int, UserInformation> userInfo = | |
new ConcurrentDictionary<int, UserInformation>(); | |
#region ConstantStr | |
private static readonly string[] Genders = new[] { "👩", "🧑" }; | |
private static readonly string[] Colors = new[] { "Red", "Blue", "Green", "Yellow", "Brown", "Black" }; | |
#endregion | |
public static void Main(string[] args) | |
{ | |
telegramBotClient.OnUpdate += async (sender, e) => | |
{ | |
await OnUpdate(e.Update); | |
}; | |
telegramBotClient.StartReceiving(new[] { UpdateType.Message }); | |
Console.ReadLine(); | |
} | |
private static async Task OnUpdate(Update up) | |
{ | |
Message message = up.Message; | |
int userid = message.From.Id; | |
if (state.ContainsKey(userid)) | |
{ | |
if(message.Text == "back") | |
{ | |
state[userid]--; | |
switch (state[userid]) | |
{ | |
case ConversationState.WitingForGender: | |
{ | |
await telegramBotClient.SendTextMessageAsync( | |
userid, | |
$"Select your gender", | |
replyMarkup: new ReplyKeyboardMarkup(GetButtons(Genders, false), true)); | |
break; | |
} | |
case ConversationState.WitingForName: | |
{ | |
await telegramBotClient.SendTextMessageAsync( | |
userid, | |
$"Enter your name:", | |
replyMarkup: new ReplyKeyboardMarkup(GetButtons(), true)); | |
break; | |
} | |
case ConversationState.WitingForFamilyname: | |
{ | |
await telegramBotClient.SendTextMessageAsync( | |
userid, | |
$"Enter your familyname", | |
replyMarkup: new ReplyKeyboardMarkup(GetButtons(), true)); | |
break; | |
} | |
case ConversationState.WitingForColor: | |
{ | |
await telegramBotClient.SendTextMessageAsync( | |
userid, | |
$"Select colors", | |
replyMarkup: new ReplyKeyboardMarkup(GetButtons(Colors, true), true)); | |
break; | |
} | |
} | |
return; | |
} | |
switch (state[userid]) | |
{ | |
case ConversationState.WitingForGender: | |
{ | |
if (!Genders.Any(x => x == message.Text)) | |
{ | |
_ = await telegramBotClient.SendTextMessageAsync( | |
userid, | |
"Invalid gender!", | |
replyMarkup: new ReplyKeyboardMarkup(GetButtons(Genders, false), true)); | |
return; | |
} | |
await telegramBotClient.SendTextMessageAsync( | |
userid, | |
$"Ok you selected {message.Text}, now what's your name?", | |
replyMarkup: new ReplyKeyboardMarkup(GetButtons(), true)); | |
state[userid] = ConversationState.WitingForName; | |
userInfo[userid].Gender = message.Text == "🧑"; | |
return; | |
} | |
case ConversationState.WitingForName: | |
{ | |
await telegramBotClient.SendTextMessageAsync( | |
userid, | |
$"Ok your name is {message.Text}, now what's your familyname?", | |
replyMarkup: new ReplyKeyboardMarkup(GetButtons(), true)); | |
state[userid] = ConversationState.WitingForFamilyname; | |
userInfo[userid].Name = message.Text; | |
return; | |
} | |
case ConversationState.WitingForFamilyname: | |
{ | |
await telegramBotClient.SendTextMessageAsync( | |
userid, | |
$"Ok your familyname is {message.Text}, now what's your Favorite colors?", | |
replyMarkup: new ReplyKeyboardMarkup(GetButtons(Colors), true)); | |
state[userid] = ConversationState.WitingForColor; | |
userInfo[userid].Familyname = message.Text; | |
return; | |
} | |
case ConversationState.WitingForColor: | |
{ | |
if (!Colors.Any(x => x == message.Text)) | |
{ | |
_ = await telegramBotClient.SendTextMessageAsync( | |
userid, | |
"Invalid color!", | |
replyMarkup: new ReplyKeyboardMarkup(GetButtons(Colors), true)); | |
return; | |
} | |
await telegramBotClient.SendTextMessageAsync( | |
userid, | |
$"Ok your Favorite colors is {message.Text}", | |
replyMarkup: new ReplyKeyboardMarkup(GetButtons(), true)); | |
state[userid] = ConversationState.Done; | |
userInfo[userid].FavoriteColors = message.Text; | |
await telegramBotClient.SendTextMessageAsync( | |
userid, | |
userInfo[userid].ToString(), | |
replyMarkup: new ReplyKeyboardRemove()); | |
return; | |
} | |
case ConversationState.Done: | |
{ | |
await telegramBotClient.SendTextMessageAsync( | |
userid, | |
$"You are done here!", | |
replyMarkup: new ReplyKeyboardMarkup(GetButtons(), true)); | |
await telegramBotClient.SendTextMessageAsync( | |
userid, | |
userInfo[userid].ToString(), | |
replyMarkup: new ReplyKeyboardRemove()); | |
return; | |
} | |
default: | |
break; | |
} | |
} | |
else | |
{ | |
await telegramBotClient.SendTextMessageAsync( | |
userid, | |
"Please select your gender", | |
replyMarkup: new ReplyKeyboardMarkup(GetButtons(Genders, false), true) | |
); | |
userInfo.TryAdd(userid, new UserInformation(userid)); | |
state.TryAdd(userid, ConversationState.WitingForGender); | |
} | |
} | |
private static IEnumerable<IEnumerable<KeyboardButton>> GetButtons(string[] vs = null, | |
bool back = true) | |
{ | |
if (vs == null) | |
{ | |
return new KeyboardButton[][] | |
{ | |
new KeyboardButton[] | |
{ | |
new KeyboardButton("back") | |
} | |
}; | |
} | |
var arraylenght = back ? vs.Length + 1 : vs.Length; | |
KeyboardButton[][] buttons = new KeyboardButton[arraylenght][]; | |
for (int i = 0; i < vs.Length; i++) | |
{ | |
buttons[i] = new KeyboardButton[] { new KeyboardButton(vs[i]) }; | |
} | |
if (back) | |
{ | |
buttons[^1] = new KeyboardButton[] { new KeyboardButton("back") }; | |
} | |
return buttons; | |
} | |
private enum ConversationState | |
{ | |
WitingForGender = 1, | |
WitingForName, | |
WitingForFamilyname, | |
WitingForColor, | |
Done | |
} | |
private class UserInformation | |
{ | |
public override string ToString() | |
{ | |
return $"{Name} {Familyname}\nGender: {Gender}\nColor: {FavoriteColors}"; | |
} | |
public UserInformation(int userid) | |
{ | |
Userid = userid; | |
} | |
public int Userid { get; set; } | |
public bool Gender { get; set; } | |
public string Name { get; set; } | |
public string Familyname { get; set; } | |
public string FavoriteColors { get; set; } | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment