public class user { [Key] public string tg_id { get; set; }// public string name { get; set; }// public string age { get; set; }// public string country { get; set; }// public string city { get; set; }// public string gender { get; set; }// public string photo { get; set; }// public string tg_username { get; set; }// -, public string tg_chat_id { get; set; }// , }
NpgSQL.EntityFrameworkCore.PostgreSQL
Microsoft.EntityFrameworkCore.Tools
public DbSet<user> user { get; set; } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { optionsBuilder.UseNpgsql(connectionString); }
enable-migrations
Add-migration * *
update-database
private static readonly TelegramBotClient Bot = new TelegramBotClient(token);// static void Main(string[] args) { var me = Bot.GetMeAsync().Result;// , ( , ) Console.Title = me.Username; // Bot.OnMessage += BotOnMessageReceived; Bot.OnCallbackQuery += BotOnCallbackQueryReceived; Bot.OnReceiveError += BotOnReceiveError; // Bot.StartReceiving(Array.Empty<UpdateType>()); Console.WriteLine($"Start listening for @{me.Username}"); Console.ReadLine(); Bot.StopReceiving(); }
BotOnMessageReceived
- the handler for receiving "normal" messagesBotOnCallbackQueryReceived
- handler for pressing buttons that appear under the message.InlineKeyboardButton
object, and all buttons need to be packaged in IEnumerable<IEnumerable<InlineKeyboardButton>>
BotOnMessageReceived
process this message and send back our buttons. if (message.Text == "/start") { var inlineKeyboard = new InlineKeyboardMarkup(new[] { new [] // first row { InlineKeyboardButton.WithCallbackData("!", "Next"), InlineKeyboardButton.WithCallbackData("", "Registration") } }); Bot.SendTextMessageAsync(message.Chat.Id, " lovebot! \r\n - /start\r\n - /register\r\n - /stats\r\n - @hahah2016", replyMarkup: inlineKeyboard); return; }
public class RegForm { public string tg_id { get; set; } public string name { get; set; } public string age { get; set; } public string country { get; set; } public string city { get; set; } public string gender { get; set; } public string photo { get; set; } public string tg_username { get; set; } public string tg_chat_id { get; set; } public int stage; public RegForm(string id, string chat_id, string username) { stage = 1; tg_id = id; tg_username = username; } public (string, int) StageText(string id) { if (stage == 1) return (" :", stage); if (stage == 2) return (" :", stage); if (stage == 3) return (" :", stage); if (stage == 4) return (" :", stage); if (stage == 5) return (" :", stage); else return (" :", stage); } public bool SetParam(string param) { if (stage == 1) name = param; if (stage == 2) age = param; if (stage == 3) country = param; if (stage == 4) city = param; if (stage == 5) gender = param; if (stage == 6) photo = param; stage++; return true; } }
static Dictionary<string, RegForm> registrations = new Dictionary<string, RegForm>();
, in which we add a new KeyValuePair, when a button is pressed.BotOnCallbackQueryReceived
var message = e.CallbackQuery; if (message.Data == "Registration") { RegForm form = new RegForm(message.From.Id.ToString(), message.Message.Chat.Id.ToString(), message.From.Username);// registrations.Add(message.From.Id.ToString(), form);// "", telegram_id . var t = form.StageText(form.tg_id); // , , . Bot.SendTextMessageAsync(message.Message.Chat.Id, t.Item1);// . return; }
using (Context db = new Context()) { IMapper mapper = new MapperConfiguration(cfg => cfg.CreateMap<RegForm, User>()).CreateMapper(); if (db.user.Where(x => x.tg_id == message.From.Id.ToString()).Count() != 0) db.user.Update(mapper.Map<RegForm, tgbot_base.classes.user>(u)); else { db.user.Add(mapper.Map<RegForm, tgbot_base.classes.user>(u)); } db.SaveChanges(); }
if (message.Type == MessageType.Photo) { string file = Bot.GetFileAsync(message.Photo[message.Photo.Count() - 1].FileId).Result.FilePath; string filepath = message.From.Id + "." + file.Split('.').Last(); using (FileStream fileStream = new FileStream("C:\\images\\" + filepath, FileMode.OpenOrCreate, FileAccess.ReadWrite)) { var st = Bot.DownloadFileAsync(file).Result; st.Position = 0; st.CopyTo(fileStream); } u.SetParam("C:\\images\\" + filepath); }
public static User GetRandom() { Stopwatch s = new Stopwatch(); s.Start(); User u; using (Context db = new Context()) { Random r = new Random(); int count = db.user.Count(); if (count > 1) count = count - 1; List<User> users = mapper.Map<List<tgbot_base.classes.user>, List<User>>(db.user.ToList()); u = users.ElementAt(r.Next(0, count)); } Console.WriteLine("[" + DateTime.Now + "] For finding " + s.ElapsedMilliseconds + " ms"); s = null; return u; }
if (message.Data == "Next") { if (searchForms.Count != 0) { searchForms.Remove(message.From.Id.ToString()); } IMapper mapper = new MapperConfiguration(cfg => cfg.CreateMap<RegForm, User>()).CreateMapper(); User user = BaseWorker.GetRandom(); SendAnket(user, message.Message.Chat.Id.ToString());//, . return; }
Source: https://habr.com/ru/post/430314/
All Articles