Example #1
0
        public async Task <IHttpActionResult> Post(string userToken, string botName, [FromBody] WebhookMessage message)
        {
            Trace.TraceInformation($"Get data from {message.id}");

            if (message.data.personEmail.Contains("sparkbot.io"))
            {
                return(Ok());
            }

            ISparkClient sparkClient = new SparkClient(await _channelsService.GetBotToken(userToken, botName));

            var text = await sparkClient.GetMessage(message.data.id);

            var nameParts  = message.name.Split(' ');
            var coreAnswer = await Talk(userToken, botName, new InputMessage()
            {
                InterlocutorId = $"{message.data.roomId}-{message.data.personId}",
                FirstName      = nameParts.FirstOrDefault(),
                LastName       = nameParts.LastOrDefault(),
                Text           = text.text,
                Username       = text.personEmail
            });

            foreach (var item in coreAnswer.Messages)
            {
                await sparkClient.SendMessage(text.roomId, item.Text);
            }

            return(Ok());
        }
        public async Task <IHttpActionResult> Get(string userToken, string botName, string roomId)
        {
            var sparkClient = new SparkClient(
                await _channelsService.GetBotToken(userToken, botName));

            var users = await sparkClient.GetUsersInRoom(roomId);

            return(Ok(users.Where(i => !i.personDisplayName.Contains("(bot)")).Select(i => new
            {
                userId = i.personId,
                userName = i.personDisplayName
            })));
        }
        public async Task <IHttpActionResult> Post(string userToken, string botName, string userId, [FromBody] TalkResponce message)
        {
            if (!userId.Contains("-"))
            {
                return(BadRequest("Invalid user id for Spark. Expected {roomId}-{peopleId}"));
            }

            var botToken = await _channelsService.GetBotToken(userToken, botName);

            var client = new SparkClient(botToken);
            var ids    = userId.Split('-');

            foreach (var item in message.Messages)
            {
                await client.SendMessage(ids[0], item.Text);
            }

            return(Ok());
        }
        // POST: api/WebhookManage
        public async Task <IHttpActionResult> Post(string userToken, string botName)
        {
            Trace.TraceInformation($"Setting webhook {userToken}:{botName}");

            var rq = new RestClient(uri)
            {
                Authenticator = new OAuth2AuthorizationRequestHeaderAuthenticator(
                    await _channelsService.GetBotToken(userToken, botName), "Bearer")
            };

            var rc = new RestRequest("webhooks", Method.POST);

            var builder = new UriBuilder("https", "ch-channel-spark.azurewebsites.net", 443, $"api/webhook/{userToken}/{botName}");
            var webhook = builder.Uri.ToString();

            Trace.TraceInformation($"Webhook to set {webhook}");

            rc.AddJsonBody(
                new
            {
                name      = $"ChatFirst WebHook for {botName}",
                targetUrl = webhook,
                resource  = "messages",
                @event    = "created",
                secret    = $"{userToken}:{botName}"  // todo hash it
            });

            var result = await rq.ExecuteTaskAsync(rc);

            Trace.TraceInformation($"Answer {result.ResponseStatus}/{result.StatusCode}: {result.Content}");

            if (result.StatusCode != HttpStatusCode.OK)
            {
                return(Ok(result.Content));
            }

            dynamic data = JObject.Parse(result.Content);

            await _channelsService.SaveWebhookId(userToken, botName, data.id);

            return(Ok(result.Content));
        }