【问题标题】:How to get user information using webhook in c#如何在c#中使用webhook获取用户信息
【发布时间】:2017-09-22 09:38:48
【问题描述】:

我正在使用 api.ai 和 webhook visual studo 2015 c#。

我已经为一些意图创建了一些动作,现在我正在寻找一个名为“welcome.input”的动作。我想获取用户的用户名。 如果用户第一次开始与机器人对话,我想让他可以查看帮助菜单或标准菜单, 当用户重新进入机器人时,我想发送文本:欢迎回来 {username} 并向他展示标准菜单。

你知道怎么做吗?

我正在阅读 https://github.com/Microsoft/BotBuilder-Samples/tree/master/CSharp/core-State 这个示例……但我无法在我的项目中添加 apt 作为 webhook。

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using WebhookReceiver.Models;

  namespace FBReceiver.Controllers
    {

public class  facebookController : ApiController
{

    public string Get()
    {
        return "OK";
    }


    public int Get(int id)
    {
        return id;
    }



    public ApiAiResponse Post([FromBody]JObject jsonRequest)
    {
        using (FbReceiverModelDataContext ctx = new 
   FbModelDataContext())
        {
            ctx.spTblTransactions_CreateNew("xyz", "Request", 
   jsonRequest.ToString(), HttpContext.Current.User.Identity.Name);

            ApiAiRequest request = jsonRequest.ToObject<ApiAiRequest>();

            ApiAiResponse response = new ApiAiResponse();

            JObject jObject = JObject.Parse(request.result.parameters.ToString());
            string xyznumber = (string)jObject["xyznumber"] != null ? (string)jObject["xyznumber"] : "";

            string otherparameter = (string)jObject["otherparameter"] != null ? (string)jObject["otherparameter"] : "";

            if (("action1".Equals(request.result.action.ToLower())))
            {
                tbla a= new tbla();
                a= ctx.tblAa.SingleOrDefault(u => u.a.ToLower() == a.ToLower());

                if (a!= null)
                {
                    response.speech = "a with number " + xyznumber+ " " + a.aaaa;

                    response.source = "aaa";
                }
                else if (!string.IsNullOrEmpty(xyznumber))
                {
                    response.speech = "Generic info about " + xyznumber;
                    response.displayText = "Generic info about " + xyznumber;
                    response.source = "aaaa";
                }
                else
                {
                    response.speech = "No info";
                    response.displayText = "No info";
                    response.source = "Parcels";
                }
            }



            else if (("pay.info".Equals(request.result.action.ToLower())))
            {
                ///yyyyyyyyyyyyyyyyyyyyyyyyyyyyy
            }


            else if (("welcome.input".Equals(request.result.action.ToLower())))
            {

                // to do 

            }


            else
            {
                response.speech = "something is wrong ????";
                response.displayText = "something is wrong ????";
                response.source = "None";
            }




            ctx.spTblTransactions_CreateNew("aaaa", "Response", JsonConvert.SerializeObject(response), HttpContext.Current.User.Identity.Name);

            return response;
        }
    }


    }
}

请帮帮我。这个话题我搜索了很多次

【问题讨论】:

  • 不清楚是什么问题。你在使用机器人框架吗?为什么不能使用注释代码?
  • 我使用的是 Api.ai,而对于 webhook,我使用的是 Visual Studio 2015,c#。注释代码不正确...我希望您的帮助获取用户机器人的用户名并在消息中使用它
  • 所以你没有使用bot框架?还是不清楚。该代码在哪里运行?如果不是机器人框架问题,请删除标签
  • 我同意,这看起来更像是“如何构建 webhook 架构”以在用户和机器人之间进行通信,而不是机器人框架问题

标签: c# facebook bots webhooks dialogflow-es


【解决方案1】:

所以你的问题有点模糊,但在概念层面上你需要做的是

  1. 存储来自消息集成(如 facebook messenger)的一些唯一用户或会话 ID
  2. 将关联名称与用户 ID 保存在某种键值映射中,或者用于数据库中更可行的生产选项。请注意,如果您想根据消息的时间间隔检查自上一条消息以来是否已经过了一段时间以执行某种“欢迎回来消息”,您还需要存储最新消息的时间戳
  3. 在消息中,检查用户是否存在,如果不是全新用户并提示输入名称,如果是,则运行时间戳检查以查看是否在一段时间后返回

这里有一个 javascript 示例来演示该方法本身,您将在收到用户的消息时调用此函数,然后将其传递给 API.ai:

        function setSessionAndUser(messageEvent, callback) {
            var senderID = messageEvent.sender.id;
            var firstTimeUser = false
            if (!sessionIds.has(senderID)) {
                sessionIds.set(senderID, uuid.v1());
            }

            if (!usersMap.has(senderID)) {
                firstTimeUser = true
                database.userData( function (user) {
                    usersMap.set(senderID, user);
                    //Note this is not touching the database and is instead temporarily storing users as a Map in server memory, it's a tradeoff of waiting to touch the DB before replying to every message vs how permanent you need the data to be (and how much data you'll be dealing with)
                    callback(messageEvent, firstTimeUser)
                }, senderID);
            } else{
                callback(messageEvent, firstTimeUser)
            }
        }

编辑为“捕获”消息事件添加示例,然后对其进行解析:

//Post to webhook to catch messages
app.post('/webhook/', function (req, res) {
    var data = req.body;

    // Make sure this is a page subscription
    if (data.object == 'page') {
        // Iterate over each entry
        // There may be multiple if batched
        data.entry.forEach(function (pageEntry) {
            var pageID = pageEntry.id;
            var timeOfEvent = pageEntry.time;

            // Iterate over each messaging event
            pageEntry.messaging.forEach(function (messagingEvent) {
                if (messagingEvent.message) {
                    receivedMessage(messagingEvent);
                }
                //Catch all
                else {
                    console.log("Webhook received unknown messagingEvent: ", messagingEvent);
                }
            });
        });

        //Must return a 200 status
        res.sendStatus(200);
    }
});

// Parsing the messageEvent
function receivedMessage(event) {
  var senderID = event.sender.id;
  //...
 }

【讨论】:

  • 我可以在 else if (("welcome.input".Equals(request.result.action.ToLower()))) { } 中使用这个函数,或者我可以在这里调用这个函数?抱歉,我是编程初学者,所以我提出了这么基本的问题。
  • 对不起,我还不清楚如何使用以及如何调用这个函数来获取用户名:(
  • 因此,您的用户名或您想要存储提供的名称的任何唯一用户标识符最终将来自消息的发件人,对吗?即 facebook messenger 将发送用户的唯一 ID 号以及消息的实际正文,因此首先您需要“捕获”这些消息,然后从正文中解析出 ID。我正在编辑答案以添加示例
  • 我用完整的代码更新了我的问题...在做部分我必须添加我的代码来捕获用户名详细信息并在消息中使用。这个想法是在 api.ai 中我只需要使用一个 controller.cs 作为 webhook 。因此,对于不同的意图,我可以使用不同的操作。也许我错了,但你的代码在我的代码中有一些问题。我可以很好地理解你的逻辑,但现在我无法用代码表达:(
猜你喜欢
  • 2012-03-16
  • 2018-08-12
  • 1970-01-01
  • 2018-07-10
  • 2020-01-15
  • 2016-07-23
  • 1970-01-01
  • 2013-03-13
  • 2020-10-14
相关资源
最近更新 更多