private Dictionary <string, string> CreateSendMessageArgs(OutcomeMessageType destination, string UserOrGroupOrChannel, string message) { var args = new Dictionary <string, string>(); switch (destination) { case OutcomeMessageType.PrivateMessage: if (!this.chatIDs.privateChatIdsByUsername.ContainsKey(UserOrGroupOrChannel)) { throw new Exception($"Can not find chat with user/group/channel {UserOrGroupOrChannel}. Firstly user/group/channel should write something to bot"); } args["chat_id"] = this.chatIDs.privateChatIdsByUsername[UserOrGroupOrChannel]; break; case OutcomeMessageType.GroupMessage: if (!this.chatIDs.groupChatIdsByGroupName.ContainsKey(UserOrGroupOrChannel)) { throw new Exception($"Can not find chat with user/group/channel {UserOrGroupOrChannel}. Firstly user/group/channel should write something to bot"); } args["chat_id"] = this.chatIDs.groupChatIdsByGroupName[UserOrGroupOrChannel]; break; case OutcomeMessageType.ChannelPost: if (!this.chatIDs.channelChatIdsByChannelName.ContainsKey(UserOrGroupOrChannel)) { throw new Exception($"Can not find chat with user/group/channel {UserOrGroupOrChannel}. Firstly user/group/channel should write something to bot"); } args["chat_id"] = this.chatIDs.channelChatIdsByChannelName[UserOrGroupOrChannel]; break; } args["text"] = message; return(args); }
private SentMessageStatus SendMessage(OutcomeMessageType destination, string UserOrGroupOrChannel, string message) { string command = "sendMessage"; string httpMethod = "POST"; SentMessageStatus result = new SentMessageStatus(); string response = null; try { var args = CreateSendMessageArgs(destination, UserOrGroupOrChannel, message); response = this.Post(httpMethod, command, args); var reader = new JsonTextReader(new System.IO.StringReader(response)); while (reader.Read()) { if (reader.Path.ToLower() == "ok" && reader.Read()) { result.Ok = bool.Parse(reader.Value.ToString()); } } } catch (Exception ex) { result.Exception = new Exception($"SendMessage failed. Data returned from telegram: {response}", ex); } return(result); }
public bool DestinationExists(OutcomeMessageType messageType, string destination) { try { this.CreateSendMessageArgs(messageType, destination, ""); } catch (Exception e) { if (e.Message.Contains("Can not find chat with")) { return(false); } } return(true); }
private string AskDestination(OutcomeMessageType destinationType) { Console.Clear(); if (destinationType == OutcomeMessageType.None) { return(null); } if (destinationType == OutcomeMessageType.PrivateMessage) { Console.WriteLine("Enter user name"); } if (destinationType == OutcomeMessageType.GroupMessage) { Console.WriteLine("Enter group name"); } if (destinationType == OutcomeMessageType.ChannelPost) { Console.WriteLine("Enter channel name"); } return(Console.ReadLine()); }