<?xml version="1.0" encoding="utf-8"?>
<AutoMateTask>
  <TaskInfo>
    <Version TaskVersion="11.6.0.70" MinVersion="0.0.0.0" AMLVersion="3.0" />
  </TaskInfo>
  <ManagedTask>
    <TaskProperties Id="{caa6557d-f36a-412e-908e-5d31f1eee542}" Name="Opsgenie Bot - Close Alerts from Excel INPUT" Path="" />
  </ManagedTask>
  <TaskVariables>
    <TaskVariable Name="TaskVar_MainPath" Default="C:\Automate\Tasks\%GetTaskName()%" />
    <TaskVariable Name="TaskVar_ElapsedTime" />
    <TaskVariable Name="TaskVar_Log" Description="%TaskVar_MainPath%\Logs\%GetTaskName()%_%format(Now(), &quot;yyyy-MM-dd hhmmss&quot;)%.log" Default="%TaskVar_MainPath%\Logs\%GetTaskName()%_%format(Now(), &quot;yyyy-MM-dd&quot;)%.log" />
    <TaskVariable Name="TaskVar_errorTitle" Default="We found an error on the task %GetTaskName()%" />
    <TaskVariable Name="TaskVar_errorDesc" />
    <TaskVariable Name="TaskVar_help" Default="%GetAutoMateDirectory()%\Help\Default.htm" />
    <TaskVariable Name="TaskVar_SuccessSubject" Default="Task %GetTaskName()% has finished succesfully." />
    <TaskVariable Name="TaskVar_Error" />
    <TaskVariable Name="var_JSONString" />
    <TaskVariable Name="var_OpsGenieApiKey" />
    <TaskVariable Name="var_OpsGenieRegionDomain" />
    <TaskVariable Name="var_OpsGenieApiURL" />
    <TaskVariable Name="var_user" />
    <TaskVariable Name="var_note" />
    <TaskVariable Name="var_ApiVersion" Default="v2" />
    <TaskVariable Name="var_ExcelInputFile" />
    <TaskVariable Name="var_Index" Default="3" />
    <TaskVariable Name="var_source" />
    <TaskVariable Name="var_AlertID" />
    <TaskVariable Name="var_RequestID" />
  </TaskVariables>
  <TaskFunctions>
    <TaskFunction Name="OnStepStart" Access="private">
      <Parameters>
        <Parameter Name="FunctionName" Description="The function that is starting the step" />
        <Parameter Name="StepNumber" Description="The step number that is about to execute" />
      </Parameters>
    </TaskFunction>
    <TaskFunction Name="OnStepEnd" Access="private">
      <Parameters>
        <Parameter Name="FunctionName" Description="The function that is ending the step" />
        <Parameter Name="StepNumber" Description="The step number that is about to end" />
      </Parameters>
    </TaskFunction>
    <TaskFunction Name="OnStepError" Access="private">
      <Parameters>
        <Parameter Name="FunctionName" Description="The function with the error" />
        <Parameter Name="StepNumber" Description="The step number of the error" />
        <Parameter Name="ErrorText" Description="The text of the error" />
        <Parameter Name="ErrorCode" Description="The code of the error" />
      </Parameters>
    </TaskFunction>
    <TaskFunction Name="OnTaskFailure" Access="private">
      <Parameters>
        <Parameter Name="FunctionName" Description="The function causing the task failure" />
        <Parameter Name="StepNumber" Description="The step number causing the task failure" />
        <Parameter Name="ErrorText" Description="The text of the error" />
        <Parameter Name="ErrorCode" Description="The code of the error" />
      </Parameters>
      <Steps>
<AMVARIABLE ACTIVITY="set" VARIABLENAME="TaskVar_error"><![CDATA[<strong>%AMError.FunctionName%</strong> in the Step <strong>%AMError.StepNumber%</strong>. Error Description: <span style="color: #ff0000;"><strong>"%AMError.ErrorDesc%"</strong></span>]]></AMVARIABLE>
<AMTIMER ACTIVITY="stop" RESULTVARIABLE="TaskVar_ElapsedTime" SESSION="TimerSession1" />
<AMFILESYSTEM ACTIVITY="write_file" FILE="%TaskVar_Log%">%NOW()% - ERROR - Error found on the Function %Replace(Replace(Replace(Replace(Replace(Replace(TaskVar_Error, "&lt;span style=""color: #ff0000;""&gt;", "", 1, -1), "&lt;p&gt;&amp;lt;", "", 1, -1), "&lt;/strong&gt;", "", 1, -1), "&lt;strong&gt;", "", 1, -1), "&lt;p&gt;&amp;lt;&lt;strong&gt;", "", 1, -1), "&lt;/span&gt;", "", 1, -1)%. 
%NOW()% - INFO - Aborting task. Total elapsed time: %Round(TaskVar_ElapsedTime, 2)% seconds.</AMFILESYSTEM>
<AMTASK ACTIVITY="stop" STATUSTYPE="ERROR" ERRORNUMBER="12345" ERRORTEXT="Error during execution." />
	</Steps>
    </TaskFunction>
    <TaskFunction Name="OnTaskStart" Access="private">
      <Parameters>
        <Parameter Name="Reason" Description="The reason the task is starting" />
      </Parameters>
      <Steps>
<!--Initiating our log-->
<AMTIMER SESSION="TimerSession1" />
<!--Writing the task start-->
<AMFILESYSTEM ACTIVITY="write_file" FILE="%TaskVar_Log%">---------------------------------------------------------------------------------------
%Now()% - INFO - Starting the task "%GetTaskName()%"</AMFILESYSTEM>
<!--Lets create our main path in case does not exist-->
<AMIF ACTIVITY="folder_exist" ACCESSIBILITYENGINE="active" FOLDER="%TaskVar_MainPath%" MODE="not_exist" />
<AMFILESYSTEM ACTIVITY="create_folder" FOLDER="%TaskVar_MainPath%" />
<AMIF ACTIVITY="end" ACCESSIBILITYENGINE="active" />
<!--Setting variable from Constants-->
<AMVARIABLE ACTIVITY="set" VARIABLENAME="var_OpsGenieApiKey"><![CDATA[%const_OpsgenieApiKey%]]></AMVARIABLE>
<AMVARIABLE ACTIVITY="set" VARIABLENAME="var_OpsGenieRegionDomain"><![CDATA[%const_OpsgenieRegionDomain%]]></AMVARIABLE>
	</Steps>
    </TaskFunction>
    <TaskFunction Name="OnTaskEnd" Access="private">
      <Parameters>
        <Parameter Name="Reason" Description="The reason the task is ending" />
      </Parameters>
      <Steps>
<!--Ending the log-->
<AMTIMER ACTIVITY="stop" RESULTVARIABLE="TaskVar_ElapsedTime" SESSION="TimerSession1" />
<AMFILESYSTEM ACTIVITY="write_file" FILE="%TaskVar_Log%">%now()% - INFO - Ending our task
%now()% - INFO - The Task %GetTaskName()% has finished without errors
%now()% - INFO - Elapsed time: %Round(TaskVar_ElapsedTime, 2)% seconds</AMFILESYSTEM>
	</Steps>
    </TaskFunction>
    <TaskFunction Name="Main" Access="private">
      <Steps>
<!--Opsgenie &#45; Excelを基にアラートをクローズ
ExcelのINPUTファイルからOpsgenieのアラートを閉じるAutoMateタスク。
By HelpSystems-->
<AMREGION NAME="設定" />
<!--アラートクロージング用のINPUT OpsGenie Excelファイルのロケーションパスと名前を設定します。-->
<AMVARIABLE ACTIVITY="set" VARIABLENAME="var_ExcelInputFile" VALUE="D:\test\Opsgenie Close Alerts INPUT.xlsx" />
<AMENDREGION />
<AMREGION NAME="Opsgenieアラートを作成する" />
<AMTASK ACTIVITY="call_function" FUNCTIONNAME="CloseOpsgenieAlerts()" />
<AMENDREGION />
	</Steps>
    </TaskFunction>
    <TaskFunction Name="CloseOpsgenieAlerts">
      <Steps>
<AMREGION NAME="Opsgenieアラートを作成する" />
<!--3行目からファイルを読み込み、Excelのデータセットに格納します。-->
<AMODS ACTIVITY="get_cell" WORKBOOK="%var_ExcelInputFile%" SAVETYPE="do_not_save" ACTION="rangebyreference" RESULTDATASET="ExcelDataset" STARTCELLREF="A3" LASTCELL="YES" />
<AMLOOP ACTIVITY="dataset" DATASET="ExcelDataset" />
<AMIF EXPRESSION="%ExcelDataset.E = ''  AND ExcelDataset.A &lt;&gt;  ''%" USECOMPLEXUI="YES" />
<AMFILESYSTEM ACTIVITY="write_file" FILE="%TaskVar_Log%">%Now()% - INFO - Closing new Case in Excel INPUT in Row %var_Index +2%.</AMFILESYSTEM>
<AMVARIABLE ACTIVITY="set" VARIABLENAME="var_AlertID"><![CDATA[%ExcelDataset.A%]]></AMVARIABLE>
<AMVARIABLE ACTIVITY="set" VARIABLENAME="var_note"><![CDATA[%ExcelDataset.B%]]></AMVARIABLE>
<AMVARIABLE ACTIVITY="set" VARIABLENAME="var_user"><![CDATA[%ExcelDataset.C%]]></AMVARIABLE>
<AMVARIABLE ACTIVITY="set" VARIABLENAME="var_source"><![CDATA[%ExcelDataset.D%]]></AMVARIABLE>
<AMVARIABLE ACTIVITY="set" VARIABLENAME="var_JSONString"><![CDATA[{
    "user":"%var_user%",
    "source":"%var_source%",
    "note":"%var_note%"
}]]></AMVARIABLE>
<AMVARIABLE ACTIVITY="set" VARIABLENAME="var_OpsGenieApiURL"><![CDATA[%var_OpsGenieRegionDomain%/%var_ApiVersion%/alerts/%var_AlertID%/close]]></AMVARIABLE>
<AMHTTP ACTIVITY="post" URL="%var_OpsGenieApiURL%" DATA="%var_JSONString%" RESULTDATASET="GetResult"><HEADER NAME="Content-Type" VALUE="application/json" /><HEADER NAME="Authorization" VALUE="GenieKey %var_OpsGenieApiKey%" /><HEADER NAME="Accept" VALUE="application/json" /></AMHTTP>
<AMIF ACTIVITY="contains_text" TEXT="%GetResult(1).ResponseHeader%" SUBSTRING="HTTP/1.1 202 Accepted" />
<AMJSON JSONSTRING="%GetResult(1).Data%" RESULTSTRUCTURE="obj_Result" />
<AMVARIABLE ACTIVITY="set" VARIABLENAME="var_RequestID"><![CDATA[%obj_Result.requestId%]]></AMVARIABLE>
<AMODS ACTIVITY="set_cell" WORKBOOK="%var_ExcelInputFile%" SETTERTYPE="list" CELLREF="E%var_Index%" LIST="YES;%Now()%;%var_RequestID%" DELIMITER="semi_colon" />
<AMFILESYSTEM ACTIVITY="write_file" FILE="%TaskVar_Log%">%Now()% - INFO - The Alert %var_AlertID% was closed, request id %var_RequestID%.</AMFILESYSTEM>
<AMELSE />
<AMODS ACTIVITY="set_cell" WORKBOOK="%var_ExcelInputFile%" SETTERTYPE="list" CELLREF="E%var_Index%" LIST="NO;%Now()%;Could not close Alert. See the log for further information." DELIMITER="semi_colon" />
<AMFILESYSTEM ACTIVITY="write_file" FILE="%TaskVar_Log%">%Now()% - ERROR - The Alert in Row %var_Index% and id %var_AlertID% could not be closed in Opsgenie. </AMFILESYSTEM>
<AMFILESYSTEM ACTIVITY="write_file" FILE="%TaskVar_Log%">%Now()% - ERROR - Error Details: %GetResult(1).Data%</AMFILESYSTEM>
<AMIF ACTIVITY="end" />
<AMIF ACTIVITY="end" />
<AMVARIABLE ACTIVITY="increment" RESULTVARIABLE="var_Index" />
<AMLOOP ACTIVITY="end" />
<AMENDREGION />
	</Steps>
    </TaskFunction>
  </TaskFunctions>
</AutoMateTask>