public async Task GetState([QueryField] bool?json, string tokenArg) { (ChatId, UserId)? token = ApiMessenger.ParseToken(tokenArg); if (token == null) { throw HttpException.Forbidden(); } var(_, user) = ((ChatId, UserId))token; using var context = new UserContext(user); var publicUser = new PublicUser(context.User); var bytes = MessagePackSerializer.Serialize(publicUser); if (json ?? false) { var response = MessagePackSerializer.ConvertToJson(bytes); HttpContext.Response.ContentType = "application/json"; bytes = Encoding.UTF8.GetBytes(response); } else { HttpContext.Response.ContentType = "application/x-msgpack"; } using (var stream = HttpContext.OpenResponseStream()) { await stream.WriteAsync(bytes, 0, bytes.Length); } }
public async Task PostMessage(string tokenArg) { (ChatId, UserId)? token = ApiMessenger.ParseToken(tokenArg); if (token == null) { throw HttpException.Forbidden(); } var(chat, user) = ((ChatId, UserId))token; var text = await HttpContext.GetRequestDataAsync <string>(); var message = new ReceivedMessage { ChatId = chat, Text = text, UserId = user }; ApiMessenger.InvokeOnMessageReceived(message); }