Ejemplo n.º 1
0
        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);
            }
        }
Ejemplo n.º 2
0
        public string Register([QueryField] string secret, [QueryField] long?id)
        {
            if (secret != ApiMessenger._secret)
            {
                throw HttpException.Forbidden();
            }

            var userId = id ?? ApiMessenger.LongRandom();

            using var context = new UserContext(new UserId(ApiMessenger.MessengerId, userId));
            return($"{userId}:{context.User.Token}");
        }
Ejemplo n.º 3
0
        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);
        }