示例#1
0
        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);
        }
示例#2
0
        public override void Execute(StateContext context)
        {
            Console.WriteLine("Sending ...");

            SentMessageStatus status = null;

            if (context.OutcomeMessageType == OutcomeMessageType.PrivateMessage)
            {
                status = context.Bot.SendMessageToUser(context.MessageDestination, context.MessageText);
            }
            else if (context.OutcomeMessageType == OutcomeMessageType.GroupMessage)
            {
                status = context.Bot.SendMessageToGroup(context.MessageDestination, context.MessageText);
            }
            else if (context.OutcomeMessageType == OutcomeMessageType.ChannelPost)
            {
                status = context.Bot.SendMessageToChannel(context.MessageDestination, context.MessageText);
            }
            context.MessageText        = null;
            context.OutcomeMessageType = OutcomeMessageType.None;
            context.MessageDestination = null;
            if (status.Ok)
            {
                Console.WriteLine("Sent");
            }
            else
            {
                Console.WriteLine(status.Exception);
            }
            if (context.Signals == Signals.SendMessageFromListening)
            {
                context.Signals = Signals.ListenToMessages;
                return;
            }
            Console.ReadKey();
            context.Signals = Signals.Exit;
            base.Execute(context);
        }