Lean  $LEAN_TAG$
Notification.cs
1 /*
2  * QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
3  * Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14 */
15 
16 using System;
17 using System.Collections.Generic;
18 using Newtonsoft.Json;
19 using QuantConnect.Util;
20 
22 {
23  /// <summary>
24  /// Local/desktop implementation of messaging system for Lean Engine.
25  /// </summary>
26  [JsonConverter(typeof(NotificationJsonConverter))]
27  public abstract class Notification
28  {
29  /// <summary>
30  /// Method for sending implementations of notification object types.
31  /// </summary>
32  /// <remarks>SMS, Email and Web are all handled by the QC Messaging Handler. To implement your own notification type implement it here.</remarks>
33  public virtual void Send()
34  {
35  //
36  }
37  }
38 
39  /// <summary>
40  /// Web Notification Class
41  /// </summary>
43  {
44  /// <summary>
45  /// Optional email headers
46  /// </summary>
47  [JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore)]
48  public Dictionary<string, string> Headers;
49 
50  /// <summary>
51  /// Send a notification message to this web address
52  /// </summary>
53  public string Address;
54 
55  /// <summary>
56  /// Object data to send.
57  /// </summary>
58  [JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore)]
59  public object Data;
60 
61  /// <summary>
62  /// Constructor for sending a notification SMS to a specified phone number
63  /// </summary>
64  /// <param name="address">Address to send to</param>
65  /// <param name="data">Data to send</param>
66  /// <param name="headers">Optional headers to use</param>
67  public NotificationWeb(string address, object data = null, Dictionary<string, string> headers = null)
68  {
69  Address = address;
70  Data = data;
71  Headers = headers;
72  }
73  }
74 
75  /// <summary>
76  /// Sms Notification Class
77  /// </summary>
79  {
80  /// <summary>
81  /// Send a notification message to this phone number
82  /// </summary>
83  public string PhoneNumber;
84 
85  /// <summary>
86  /// Message to send. Limited to 160 characters
87  /// </summary>
88  [JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore)]
89  public string Message;
90 
91  /// <summary>
92  /// Constructor for sending a notification SMS to a specified phone number
93  /// </summary>
94  /// <param name="number"></param>
95  /// <param name="message"></param>
96  public NotificationSms(string number, string message)
97  {
98  PhoneNumber = number;
99  Message = message;
100  }
101  }
102 
103  /// <summary>
104  /// Email notification data.
105  /// </summary>
107  {
108  /// <summary>
109  /// Optional email headers
110  /// </summary>
111  [JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore)]
112  public Dictionary<string, string> Headers;
113 
114  /// <summary>
115  /// Send to address:
116  /// </summary>
117  public string Address;
118 
119  /// <summary>
120  /// Email subject
121  /// </summary>
122  public string Subject;
123 
124  /// <summary>
125  /// Message to send.
126  /// </summary>
127  [JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore)]
128  public string Message;
129 
130  /// <summary>
131  /// Email Data
132  /// </summary>
133  [JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore)]
134  public string Data;
135 
136  /// <summary>
137  /// Default constructor for sending an email notification
138  /// </summary>
139  /// <param name="address">Address to send to. Will throw <see cref="ArgumentException"/> if invalid
140  /// <see cref="Validate.EmailAddress"/></param>
141  /// <param name="subject">Subject of the email. Will set to <see cref="string.Empty"/> if null</param>
142  /// <param name="message">Message body of the email. Will set to <see cref="string.Empty"/> if null</param>
143  /// <param name="data">Data to attach to the email. Will set to <see cref="string.Empty"/> if null</param>
144  /// <param name="headers">Optional email headers to use</param>
145  public NotificationEmail(string address, string subject = "", string message = "", string data = "", Dictionary<string, string> headers = null)
146  {
147  if (!Validate.EmailAddress(address))
148  {
149  throw new ArgumentException(Messages.NotificationEmail.InvalidEmailAddress(address));
150  }
151 
152  Address = address;
153  Data = data ?? string.Empty;
154  Message = message ?? string.Empty;
155  Subject = subject ?? string.Empty;
156  Headers = headers;
157  }
158  }
159 
160  /// <summary>
161  /// Telegram notification data
162  /// </summary>
164  {
165  /// <summary>
166  /// Send a notification message to this user on Telegram
167  /// Can be either a personal ID or Group ID.
168  /// </summary>
169  public string Id;
170 
171  /// <summary>
172  /// Message to send. Limited to 4096 characters
173  /// </summary>
174  [JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore)]
175  public string Message;
176 
177  /// <summary>
178  /// Token to use
179  /// </summary>
180  [JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore)]
181  public string Token;
182 
183  /// <summary>
184  /// Constructor for sending a telegram notification to a specific User ID
185  /// or group ID. Note: The bot must have an open chat with the user or be
186  /// added to the group for messages to deliver.
187  /// </summary>
188  /// <param name="id">User Id or Group Id to send the message too</param>
189  /// <param name="message">Message to send</param>
190  /// <param name="token">Bot token to use, if null defaults to "telegram-token"
191  /// in config on send</param>
192  public NotificationTelegram(string id, string message, string token = null)
193  {
194  Id = id;
195  Message = message;
196  Token = token;
197  }
198  }
199 }