I have a problem with my code.
I got the code from a tutorial and at first it worked fine, but then it stopped working and got the error code text was "null".
Here is my code:
private static void Client_OnMessage(object sender, Telegram.Bot.Args.MessageEventArgs e)
{
var id = e.Message.Chat.Id;
var text = e.Message.Text;
text = text.Split(' ')[1]; //error right here!
switch (text)
{
case "now":
var response = DateTime.Now.ToString();
Client.SendTextMessageAsync(id, response);
break;
}
}
EDIT:
I tried something else and got another error, maybe im one step foward?!
System.IndexOutOfRangeException: "Index was outside the bounds of the array.
''' var id = e.Message.Chat.Id; var text = e.Message.Text ?? "";
text = text.Split(' ')[1] ?? "";
switch (text)
'''
CodePudding user response:
change this line
var text = e.Message.Text
to
var text = e.Message.Text ?? "";
then change
text = text.Split(' ')[1];
to
if(text.Split(' ').Length >= 2)
text = text.Split(' ')[1];
CodePudding user response:
maybe there is only one word in "text" and you need to w