简体   繁体   English

与Bot Framefork进行群聊的常用列表

[英]Common list for a group chat with bot framefork

i want to build simple Telegram bot with Microsoft Bot Framework (C#) What i'm trying to do now is to create a list that can be filled by everyone in a group chat. 我想用Microsoft Bot Framework(C#)构建一个简单的Telegram机器人。我现在想做的是创建一个列表,每个人在群聊中都可以填写。 The result that i want: 我想要的结果:

user a: /add hello
user b: /add world
user a: /show
bot: hello world
user b: /show
bot: hello world

but what i have for now is: 但是我现在所拥有的是:

user a: /add hello
user b: /add world
user a: /show
bot: hello
user b: /show
bot: world

[Serializable]
public class RootDialog : IDialog<object>
{
    private List<string> list = new List<string>();

    public Task StartAsync(IDialogContext context)
    {
        context.Wait(MessageReceivedAsync);

        return Task.CompletedTask;
    }

    private async Task MessageReceivedAsync(IDialogContext context, IAwaitable<object> result)
    {
        var activity = await result as Activity;
        switch (Parser.GetCommand(activity))
        {
            case "/add":
                AddNewThingToList(activity);
                break;
            case "/show":
                ShowList(context);
                break;
        }

        context.Wait(MessageReceivedAsync);
    }

    private void AddNewThingToList(Activity activity)
    {
        list.Add(Parser.GetAfterCommandText(activity));
    }

    private void ShowList(IDialogContext context)
    {
        var response = new StringBuilder();

        for (var i = 0; i < list.Count; i++)
        {
            response.Append($"{i}. {list[i]}{Environment.NewLine}");
        }

        context.PostAsync(response.ToString());
    }
}

what is the most simple way to create a common list? 创建通用列表的最简单方法是什么?

It sounds like you need the list in a separate, static list to maintain a single list between all the instances of your root dialog. 听起来您需要一个单独的静态列表中的列表,才能在根对话框的所有实例之间维护一个列表。 Something like the following that can be accessed by RootDialog : RootDialog可以访问以下RootDialog

static class ListTest
{
    static List<string> _list; // Static List instance

    static ListTest()
    {
        _list = new List<string>();
    }

    public static void AddNewThingToList(Activity activity)
    {
        _list.Add(Parser.GetAfterCommandText(activity));
    }

    public static void ShowList()
    {
        var response = new StringBuilder();

        for (var i = 0; i < _list.Count; i++)
        {
            response.Append($"{i}. {_list[i]}{Environment.NewLine}");
        }
        return response;
    }
}

There will have to be considerations for multiple readers / writers accessing the list, but the code should get you pointed toward a solution. 必须有多个读取器/写入器访问该列表的注意事项,但是代码应该使您指出解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM