Send a proactive message to user via api controller

空扰寡人 提交于 2020-01-16 10:04:26

问题


I am trying to make a reminder feature for a chat bot. I thought of using azure function to make an api post request when it matches a date. I made a controller with this guide i found.

I tried it on bot emulator and found that it only works like this: Run code and bot emulator then make a request on fiddler but nothing happens. Then i stop running the code and run it again but not to restart the emulator. Then i make a fiddler request again and now it successfully sends a proactive message. And i also cannot continue a dialog. What i want to achieve is when the proactive message is sent, the user can still continue the dialog if the user is in a middle of a dialog.

OnTurnAsync()

        var conversationReference = turnContext.Activity.GetConversationReference();
        var filepath = Path.Combine(_hostingEnvironment.WebRootPath, "resume.json");
        File.WriteAllText(filepath, JsonConvert.SerializeObject(conversationReference));

Controller

namespace BasicBot.Controllers
{
    [Route("api/callBack")]
    public class CallBackController : Controller
    {
        private readonly IHostingEnvironment _hostingEnvironment;

        public CallBackController(IHostingEnvironment hostingEnvironment)
        {
            this._hostingEnvironment = hostingEnvironment;
        }

        [HttpPost]
        public async Task<HttpResponseMessage> Post([FromBody] ProactiveMessage message)
        {
            var filepath = Path.Combine(_hostingEnvironment.WebRootPath, "resume.json");

            if (System.IO.File.Exists(filepath))
            {
                var resumeJson = System.IO.File.ReadAllText(filepath);
                var resumeData = JsonConvert.DeserializeObject<ConversationReference>(resumeJson);
                var client = new ConnectorClient(new Uri(resumeData.ServiceUrl));
                var message1 = resumeData.GetContinuationActivity().CreateReply($"Try send proactive message");
                await client.Conversations.ReplyToActivityAsync((Activity)message1);
                return new HttpResponseMessage(System.Net.HttpStatusCode.OK);

            }
            else
            {
                return new HttpResponseMessage(System.Net.HttpStatusCode.BadRequest);
            }
        }
    }

    public class ProactiveMessage
    {
        public string Text { get; set; }
    }
}

来源:https://stackoverflow.com/questions/57412057/send-a-proactive-message-to-user-via-api-controller

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!