Beispiel #1
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            loggerFactory
            .AddConsole(Configuration.GetSection("Logging"))
            .AddFile(Configuration.GetSection("FileLogging"));

            RocketChatSDK.Logger = loggerFactory.CreateLogger <RocketChatSDK>();
            MongoHelper.Logger   = loggerFactory.CreateLogger("MongoHelper");

            Task.Run(async() =>
            {
                try
                {
                    var login           = await RocketChatSDK.Login(RocketChatSDK.Settings.APIUserName, RocketChatSDK.Settings.APIPassword);
                    RocketChatSDK.Admin = new RocketChatSDK(login.Data.UserId, login.Data.AuthToken);
                }
                catch (System.Exception ex)
                {
                    RocketChatSDK.Logger.LogError(new EventId((int)LoggerEventId.ROCKET_CHAT_SDK_INIT_ERROR), ex, "Rocket Chat SDK Init Error: {0}", ex.Message);
                }
            }).Wait();

            app.UseMvc(routes => routes.MapRoute("default", "api/{controller=Conversation}/{action=Chat}/{id?}"));

            app.UseStaticFiles();
        }
Beispiel #2
0
        public async Task <ActionResult> UserInput(string CHAT_USER_ID, string CHAT_USER_TOKEN, string TEXT, string AGENT)
        {
            var client = new RocketChatSDK(CHAT_USER_ID, CHAT_USER_TOKEN);
            await client.PostMessage(TEXT, "@" + AGENT);

            return(Ok(new { }));
        }
Beispiel #3
0
        public async Task <ActionResult> SubmitHistory([FromBody] SubmitChatHistoryModel history)
        {
            if (string.IsNullOrWhiteSpace(history.CHAT_USER_ID) || string.IsNullOrWhiteSpace(history.CHAT_USER_TOKEN))
            {
                return(BadRequest("Empty chat user id or chat user token"));
            }
            if (history == null || history.HISTORY == null || history.HISTORY.Count <= 0)
            {
                return(Ok());
            }

            try
            {
                var merchant = new RocketChatSDK(history.CHAT_USER_ID, history.CHAT_USER_TOKEN);

                foreach (var section in history.HISTORY.OrderBy(x => (int)x["Sno"]))
                {
                    PostMessageRequest req = null;
                    switch (section["SectionType"].ToString().ParseEnum <SectionTypeEnum>())
                    {
                    case SectionTypeEnum.Text:
                        var txtSection = section.ToObject <TextSection>();
                        if (!txtSection.Hidden)
                        {
                            req = new PostMessageRequest
                            {
                                Text = txtSection.Text
                            }
                        }
                        ;
                        break;

                    case SectionTypeEnum.Image:
                        var imgSection = section.ToObject <ImageSection>();
                        if (!imgSection.Hidden)
                        {
                            req = new PostMessageRequest
                            {
                                Text        = null,
                                Attachments = new[]
                                {
                                    new Attachment
                                    {
                                        Collapsed = false,
                                        ImageUrl  = imgSection.Url,
                                        Title     = imgSection.Title,
                                        Text      = imgSection.Caption
                                    }
                                }
                            }
                        }
                        ;
                        break;

                    case SectionTypeEnum.Gif:
                        var gifSection = section.ToObject <GifSection>();

                        if (!gifSection.Hidden)
                        {
                            req = new PostMessageRequest
                            {
                                Text        = null,
                                Attachments = new[]
                                {
                                    new Attachment
                                    {
                                        Collapsed = false,
                                        ImageUrl  = gifSection.Url,
                                        Title     = gifSection.Title,
                                        Text      = gifSection.Caption
                                    }
                                }
                            }
                        }
                        ;
                        break;

                    case SectionTypeEnum.Video:
                        var vidSection = section.ToObject <VideoSection>();
                        if (!vidSection.Hidden)
                        {
                            req = new PostMessageRequest
                            {
                                Text        = null,
                                Attachments = new[]
                                {
                                    new Attachment
                                    {
                                        Collapsed = false,
                                        VideoUrl  = vidSection.Url,
                                        Title     = vidSection.Title,
                                        Text      = vidSection.Caption
                                    }
                                }
                            }
                        }
                        ;
                        break;

                    default:
                        req = new PostMessageRequest
                        {
                            Text = $"[Unsupported section type: {section["SectionType"]}]",
                        };
                        break;
                    }
                    if (section["Direction"].ToString().ParseEnum <MessageDirection>() == MessageDirection.Out)
                    {
                        req.Channel = "@" + history.AGENT;
                        await merchant.PostMessage(req);
                    }
                    else if (section["Direction"].ToString().ParseEnum <MessageDirection>() == MessageDirection.In)
                    {
                        var room = (await RocketChatSDK.Admin.GetInstantMessageThreads()).Ims.Where(x => x.Usernames != null).FirstOrDefault(x => x.Usernames.Contains(history.AGENT) && x.Usernames.Contains(history.DEVICE_ID));
                        if (room == null)
                        {
                            await merchant.PostMessage("----", "@" + history.AGENT);

                            room = (await RocketChatSDK.Admin.GetInstantMessageThreads()).Ims.Where(x => x.Usernames != null).FirstOrDefault(x => x.Usernames.Contains(history.AGENT) && x.Usernames.Contains(history.DEVICE_ID));
                        }
                        if (room != null)
                        {
                            req.Alias  = "ANA Bunny";
                            req.RoomId = room.Id;
                            req.Avatar = $"{Request.Scheme}://{Request.Host}{(string.IsNullOrWhiteSpace(Request.PathBase.ToString()) ? "" : $"/{Request.PathBase}")}/favicon.ico";
                            await RocketChatSDK.Admin.AdminPostMessage(req);
                        }