示例#1
0
        public TBotConfing ParseAPIConf(string jsonFile)
        {
            try
            {
                using (StreamReader r = new StreamReader(jsonFile))
                {
                    string      json           = r.ReadToEnd();
                    TBotConfing botConf        = new TBotConfing();
                    JObject     jObject        = JObject.Parse(json);
                    JToken      telegramBotAPI = jObject["TelegramBotAPI"];
                    botConf.SetTelegramBotKEY((string)telegramBotAPI["KEY"]);
                    JToken googleAPI = jObject["GoogleAPI"];
                    botConf.SetGoogleCX((string)googleAPI["CX"]);
                    botConf.SetGoogleKEY((string)googleAPI["KEY"]);
                    botConf.SetBotMaster((string)jObject["Master"]);

                    long chatID = 0;

                    if (long.TryParse(jObject["ChatGroupID"].ToString(), out chatID))
                    {
                        botConf.SetChatGroupID(chatID);
                    }

                    return(botConf);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
示例#2
0
        static void Main(string[] args)
        {
            try
            {
                botConfig = new TBotConfigParser().ParseAPIConf("TBotConf.json");

                db = new TBotDataBase();
                db.InitDBAsync().Wait();

                if (db.UsersCount() == 0)
                {
                    Console.WriteLine(TBotStrings.TBotNoUsersOnDB);
                    db.InsertUser(botConfig.BotMaster, true);
                }
                else
                {
                    TBotUser admin = db.GetUser(1);
                    if (!admin.Username.Equals(botConfig.BotMaster))
                    {
                        Console.WriteLine(string.Format(TBotStrings.TBotNotThisBotAdmin, db.GetDBName()));
                    }
                }

                listUsers = db.GetUserListAsync().Result;

                chatBot = factory.Create(ChatterBotType.PANDORABOTS, "f5d922d97e345aa1");

                chatBotSession = chatBot.CreateSession();

                tBot = new TelegramBotClient(botConfig.TelegramBotAPIKEY);

                var me = tBot.GetMeAsync().Result;
                botNick         = me.FirstName;
                Console.Title   = botNick;
                tBot.OnMessage += TBot_OnMessage;

                tBot.StartReceiving(Array.Empty <UpdateType>());

                if (botConfig.ChatGroupID != 0)
                {
                    Task <bool> callTask = Task.Run(() => SendMsgToTelegram(botConfig.ChatGroupID, string.Format(TBotStrings.TBotGreetingMSG, me.FirstName)));

                    callTask.Wait();
                    bool greetingSent = callTask.Result;

                    if (greetingSent)
                    {
                        Console.WriteLine("Greetings sent");
                    }
                }

                Console.WriteLine($"I am user {me.Id} and my Username is {me.Username}.\nStart listening for @{me.FirstName}");
                Console.ReadLine();
                tBot.StopReceiving();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }