Esempio n. 1
0
        public bool insertOrUpdate(Kv kv)
        {
            string        sql     = $"insert or replace into {tbName_} (k,v,r) values (:k,:v,:r)";
            SQLiteCommand command = new SQLiteCommand(sql, conn_);

            command.Parameters.Add(new SQLiteParameter(":k", kv.k));
            command.Parameters.Add(new SQLiteParameter(":v", kv.v));
            command.Parameters.Add(new SQLiteParameter(":r", kv.r));
            command.ExecuteNonQuery();
            return(true);
        }
Esempio n. 2
0
        public List <Kv> fuzzyQuery(string k)
        {
            List <Kv>     result = new List <Kv>();
            string        sql    = $"select * from {tbName_} where k like :k";
            SQLiteCommand cmd    = new SQLiteCommand(sql, conn_);

            cmd.Parameters.Add(new SQLiteParameter(":k", $"%{k}%"));
            SQLiteDataReader dr = cmd.ExecuteReader();

            if (dr != null && dr.Read())
            {
                Kv kv = new Kv();
                kv.k = dr["k"] as string;
                kv.v = dr["v"] as string;
                kv.r = dr["r"] as string;
                result.Add(kv);
            }

            return(result);
        }
Esempio n. 3
0
        async Task <Kv> processDefine(Message msg, Sender sender)
        {
            string ques   = "";
            string ans    = "";
            string ansimg = "";

            foreach (var ele in msg.data)
            {
                if (ele is ElementText)
                {
                    ElementText elet = ele as ElementText;
                    if (!elet.text.TrimStart().StartsWith(keyword))
                    {
                        continue;
                    }
                    string   defstr = elet.text.Trim().Substring(keyword.Length).Replace(":", ":");
                    string[] defarr = defstr.Split(':', options: StringSplitOptions.RemoveEmptyEntries);

                    if (defarr.Length == 0)
                    {
                        return(null);
                    }

                    ques = defarr[0];

                    if (ques.Contains(keyword))
                    {
                        return(null);
                    }

                    if (defstr.EndsWith(':') && defarr.Length == 1)
                    {
                        // define image
                        continue;
                    }
                    if (defarr.Length != 2)
                    {
                        return(null);
                    }

                    ans = defarr[1];
                    break;
                }
                if (ele is ElementImage && ques != "")
                {
                    ElementImage elei = ele as ElementImage;
                    if (!await elei.Fix())
                    {
                        return(null);
                    }
                    ansimg = elei.data["file"];
                    break;
                }
            }
            if (ques == "" || (ans == "" && ansimg == ""))
            {
                return(null);
            }

            Tag tag = new Tag
            {
                anstype     = ans != "" ? Tag.ansenum.text : Tag.ansenum.image,
                trainerid   = sender.user_id,
                trainername = sender.nickname
            };

            Kv kv = new Kv
            {
                k = ques,
                v = ans != "" ? ans : ansimg,
                r = JsonConvert.SerializeObject(tag)
            };

            return(kv);
        }
Esempio n. 4
0
        async public Task <bool> ProcessAsync(CQApiClient client, MessageEvent me, long srcid)
        {
            if (Global.msgFilter(me.message, true, s => s.TrimStart().StartsWith(keyword)))
            {
                Kv kv = await processDefine(me.message, me.sender);

                if (kv == null)
                {
                    await client.SendMessageAsync(me.messageType, srcid, new Message(
                                                      new ElementAt(me.sender.user_id),
                                                      new ElementText(" 识别不了定义呢")));

                    return(true);
                }

                try
                {
                    kvt_.insertOrUpdate(kv);

                    await client.SendMessageAsync(me.messageType, srcid, new Message(
                                                      new ElementAt(me.sender.user_id),
                                                      new ElementText($" 学会 {kv.k} 了!")));
                }
                catch (Exception e)
                {
                    await client.SendMessageAsync(me.messageType, srcid, new Message(
                                                      new ElementAt(Global.masterid),
                                                      new ElementText($" 数据库插不进去:{e.Message}")));
                }
                return(true);
            }

            if (!Global.msgFilter(me.message, true, s => true))
            {
                return(false);
            }

            Kv ans = processQuestion(Global.msgText(me.message));

            if (ans == null)
            {
                return(false);
            }

            Tag tag = JsonConvert.DeserializeObject <Tag>(ans.r);

            if (tag.anstype == Tag.ansenum.text)
            {
                await client.SendMessageAsync(me.messageType, srcid, new Message(
                                                  new ElementAt(me.sender.user_id),
                                                  new ElementText(" " + ans.v)));

                return(true);
            }
            else if (tag.anstype == Tag.ansenum.image)
            {
                await client.SendMessageAsync(me.messageType, srcid, new Message(
                                                  new ElementAt(me.sender.user_id),
                                                  new ElementImage(ans.v)));

                return(true);
            }

            return(false);
        }