使用发布/订阅时,交互按钮无法正常工作 [英] Interactive button doesn't work properly when using pub/sub

查看:86
本文介绍了使用发布/订阅时,交互按钮无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用C#编写一个使用pub/sub的环聊聊天机器人,因此我可以将该机器人托管在防火墙这一侧.除了卡中的交互式按钮之外,其他所有东西似乎都运行良好.如果我创建具有特定操作方法名称的按钮,则该漫游器确实会收到具有适当操作方法名称的CARD_CLICKED消息.但是,环聊聊天应用程序中的卡似乎并不知道已发送响应,因为该机器人最终在环聊聊天应用程序最终说无法联系Bot.请稍后再试"之前收到了CARD_CLICKED消息三遍.我一直在使用NuGet的Google.Apis.HangoutsChat.v1和Google.Cloud.PubSub.V1软件包作为机器人.

I'm writing a Hangouts Chat bot in C# that uses pub/sub so I can host the bot on our side of a firewall. Everything seems to work well except interactive buttons within cards. If I create a button with a specific action method name, the bot does receive the CARD_CLICKED message with the appropriate action method name. However, it doesn't seem like the card in the Hangouts Chat app knows a response was sent because the bot ends up getting the CARD_CLICKED message three times before the Hangouts Chat app finally says "Unable to contact Bot. Try again later". I've been using the Google.Apis.HangoutsChat.v1 and Google.Cloud.PubSub.V1 packages from NuGet for the bot.

这是推测,但似乎问题在于交互按钮无法通过pub/sub正常工作.任何帮助将不胜感激.

This is speculation, but it seems like the issue might be that interactive buttons don't work properly through pub/sub. Any help would be appreciated.

这是我拥有的代码的片段:

Here is a snippet of the code I have:

SubscriptionName subscriptionName = new SubscriptionName(PROJECT_ID, SUBSCRIPTION_ID);
SubscriberServiceApiClient client = SubscriberServiceApiClient.Create();

GoogleCredential credential = GoogleCredential.FromFile(CREDENTIALS_PATH_ENV_PROPERTY).CreateScoped(HANGOUTS_CHAT_API_SCOPE);
HangoutsChatService chatService = new HangoutsChatService(new BaseClientService.Initializer
{
    ApplicationName = "My Bot",
    HttpClientInitializer = credential
});

while (true)
{
    PullResponse response = client.Pull(subscriptionName, false, 3, CallSettings.FromCallTiming(CallTiming.FromExpiration(Expiration.FromTimeout(TimeSpan.FromSeconds(90)))));

    if ((response.ReceivedMessages == null) || (response.ReceivedMessages.Count == 0))
        Console.WriteLine("Pulled no messages.");
    else
    {
        foreach (ReceivedMessage message in response.ReceivedMessages)
        {
            try
            {
                byte[] jsonBytes = message.Message.Data.ToByteArray();
                JObject json = JObject.Parse(Encoding.UTF8.GetString(jsonBytes));

                string messageType = (string)json["type"];
                switch (messageType)
                {
                    case "MESSAGE":
                    {
                        // Get text
                        string messageText = (string)json["message"]["text"];
                        Console.WriteLine($"[{messageType}] {messageText}");

                        // Send response
                        string spaceName = (string)json["space"]["name"];
                        SpacesResource.MessagesResource.CreateRequest request = chatService.Spaces.Messages.Create(new Message
                        {
                            Cards = new[]
                            {
                                new Card
                                {
                                    Header = new CardHeader
                                    {
                                        Title = "Message Received!"
                                    },
                                    Sections = new[]
                                    {
                                        new Section
                                        {
                                            Widgets = new[]
                                            {
                                                new WidgetMarkup
                                                {
                                                    Buttons = new[]
                                                    {
                                                        new Button
                                                        {
                                                            TextButton = new TextButton
                                                            {
                                                                Text = "Click Me!",
                                                                OnClick = new OnClick
                                                                {
                                                                    Action = new FormAction
                                                                    {
                                                                        ActionMethodName = "ClickedAction"
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            },
                            Thread = new Thread
                            {
                                Name = (string)json["message"]["thread"]["name"]
                            }
                        }, spaceName);
                        Message responseMsg = request.Execute();

                        break;
                    }
                    case "CARD_CLICKED":
                    {
                        string actionMethodName = (string)json["action"]["actionMethodName"];
                        Console.WriteLine($"[{messageType}] {actionMethodName} at {((DateTime)json["message"]["createTime"]).ToString()}");

                        // Send response
                        string spaceName = (string)json["space"]["name"];
                        SpacesResource.MessagesResource.CreateRequest request = chatService.Spaces.Messages.Create(new Message
                        {
                            ActionResponse = new ActionResponse
                            {
                                Type = "UPDATE_MESSAGE"
                            },
                            Text = $"You clicked on '{actionMethodName}'.",
                            Thread = new Thread
                            {
                                Name = (string)json["message"]["thread"]["name"]
                            }
                        }, spaceName);
                        Message responseMsg = request.Execute();

                        break;
                    }
                    default:
                    {
                        Console.WriteLine($"[{messageType}]");
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error parsing message: {ex}");
            }
        }

        // Acknowledge the message so we don't see it again.
        string[] ackIds = new string[response.ReceivedMessages.Count];
        for (int i = 0; i < response.ReceivedMessages.Count; ++i)
            ackIds[i] = response.ReceivedMessages[i].AckId;
        client.Acknowledge(subscriptionName, ackIds);
    }
}

推荐答案

在环聊聊天API中使用按钮需要自定义答案,包括:

Using buttons with Hangouts Chat API requires a custom answer including:

{
    'thread': {
        'name': thread_id
    },
    'actionResponse': {
        'type': 'UPDATE_MESSAGE'
    }
}

我建议将环聊聊天API与机器人网址一起使用.

I'd recommend using Hangouts Chat API with a bot URL.

这篇关于使用发布/订阅时,交互按钮无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆