예제 #1
0
        /// <summary>
        /// botの初期化処理
        /// </summary>
        /// <remarks>各botのタイマーを設定する。</remarks>
        private void Initialize()
        {
            // メンバ変数の初期化
            botList         = new List <TwitterBot>();
            randomTimerList = new List <Timer>();
            replyTimerList  = new List <Timer>();

            /*
             * サービスの場合はカレントディレクトリがプログラムの配置場所と異なる。
             * 設定ファイルをカレントパスで指定できるようにプログラム配置場所を
             * カレントディレクトリに設定する。
             */
            string currPath =
                Directory.GetParent(System.Reflection.Assembly.GetEntryAssembly().Location).ToString();

            // 設定ファイルからbot情報一覧を抽出
            TwitterBotInfoList xmlData = new TwitterBotInfoList();

            System.Environment.CurrentDirectory = currPath;
            XmlReader.XmlReader.GetInstance.Read(@"Setting\TwitterBotSetting.xml", ref xmlData);

            try
            {
                foreach (TwitterBotInfo twitterBot in xmlData.TwitterBotList)
                {
                    // bot情報からTwitterBotインスタンスを生成
                    TwitterBot bot = TwitterBot.CreateTwitterBot(twitterBot.BotName,
                                                                 twitterBot.ConsumerKey,
                                                                 twitterBot.ConsumerSecret,
                                                                 twitterBot.AccessToken,
                                                                 twitterBot.AccessTokenSecret,
                                                                 Int32.Parse(twitterBot.RandomTimer),
                                                                 Int32.Parse(twitterBot.ReplyTimer));
                    botList.Add(bot);

                    // ランダムポストのタイマー生成
                    Timer randomTimer = new Timer();
                    randomTimer.Elapsed  += RandomExecute;
                    randomTimer.Enabled   = true;
                    randomTimer.AutoReset = true;
                    randomTimer.Interval  = bot.RandomTimer * 1000 * 60;
                    randomTimerList.Add(randomTimer);

                    // リプライタイマー生成
                    Timer replyTimer = new Timer();
                    replyTimer.Elapsed  += ReplyExecute;
                    replyTimer.Enabled   = true;
                    replyTimer.AutoReset = true;
                    replyTimer.Interval  = bot.ReplyTimer * 1000 * 60;
                    replyTimerList.Add(replyTimer);
                }
            }
            catch
            {
                // ボットの初期処理に失敗した場合はアプリケーションを落とす。
                OnStop();
            }
        }
예제 #2
0
        /// <summary>
        /// Botの生成処理を行う。
        /// </summary>
        /// <param name="botName">Bot名称</param>
        /// <param name="consumerKey">コンシューマキー</param>
        /// <param name="consumerSecret">コンシューマシークレット</param>
        /// <param name="accessToken">アクセストークン</param>
        /// <param name="accessTokenSecret">アクセストークンシークレット</param>
        /// <param name="randomTimer">ランダムタイマー</param>
        /// <param name="replyTimer">リプライタイマー</param>
        /// <returns>作成したTwitterBotインスタンス</returns>
        /// <remarks>Botのプロパティの設定を行い、トークンを作成する。</remarks>
        /// <exception cref="InvalidOperationException">トークンの作成に必要な情報に誤りがある場合。</exception>
        public static TwitterBot CreateTwitterBot(
            string botName,
            string consumerKey,
            string consumerSecret,
            string accessToken,
            string accessTokenSecret,
            int randomTimer,
            int replyTimer)
        {
            TwitterBot bot = new TwitterBot();

            // Botの初期化を行う。
            bot.BotName = botName;
            bot.ConsumerKey = consumerKey;
            bot.ConsumerSecret = consumerSecret;
            bot.AccessToken = accessToken;
            bot.AccessTokenSecret = accessTokenSecret;
            bot.RandomTimer = randomTimer;
            bot.ReplyTimer = replyTimer;

            // 辞書ファイルのファイル名を設定
            bot.randomDicFile = settingDir + "Random" + bot.BotName + "Dic.txt";
            bot.replyDicFile = settingDir + "Reply" + bot.BotName + "Dic.xml";
            bot.replySettingFile = settingDir + "Reply" + bot.BotName;
            bot.tlReplySettingFile = settingDir + "TLReply" + bot.BotName;

            // トークン生成
            bot.token = Tokens.Create(bot.ConsumerKey,
                bot.ConsumerSecret,
                bot.AccessToken,
                bot.AccessTokenSecret);

            try
            {
                // 正常にアクセスできるか確認
                bot.token.Statuses.HomeTimeline(count => 0);
            }
            catch
            {
                throw new InvalidOperationException("トークンの生成に失敗しました。\n" +
                    "ConsumerKey,ConsumerSecret,AccessToken,AccessTokenSecretが正しいか確認してください。");
            }

            return bot;
        }