Closed erroe closed 8 years ago
If the user presses the custom keyboard button with RequestContact = true
you will receive an update with Update.Type
== MessageUpdate
and the contact information in Update.Message.Contact
I have Bot with 2 button. "about us" and "request contact". I use BotOnMessageReceived to Reserved my new message(your example on github) whereas for received request_contact I need to use getUpdates(your above answer). I want to when user select botton "about us", sent some text to him.
if (message.Text == ("about us"))
{
await Bot.SendTextMessageAsync(message.Chat.Id, "information about us",
replyMarkup: keyboard);
}
and when he select button "request contact", sent his phone nubmer to him.
static async Task GetContactPhone()
{
// var offset = 0;
var Updates = await Bot.GetUpdates();
foreach (var update in Updates)
{
Console.WriteLine("aaaa");
if (update.Type == UpdateType.MessageUpdate)
{
Console.WriteLine("bbb");
var cc = update.Message.Contact.PhoneNumber;
//string ph = message.Contact.PhoneNumber;
await Bot.SendTextMessageAsync(update.Message.Chat.Id, cc);
break;
}
// offset = update.Id + 1;
}
}
how can I do this(have 2 incoming message. which one is text message and other one is request_contact, and I want to do proper reaction for them)? how can I set if statement condition to found when "request contact" button pressed?
this is my new code:
namespace kalayab20bot
{
class Program
{
private static readonly TelegramBotClient Bot = new TelegramBotClient("my token");
static void Main(string[] args)
{
/*Bot.OnMessage += BotOnMessageReceived;
Bot.OnMessageEdited += BotOnMessageReceived;
Bot.OnReceiveError += BotOnReceiveError;
var me = Bot.GetMeAsync().Result;
Console.Title = me.Username;
Bot.StartReceiving();
Console.ReadLine();
Bot.StopReceiving();*/
try
{
GetContactPhone().Wait();
}
catch (AggregateException e)
{
throw e.InnerException;
}
}
private static void BotOnReceiveError(object sender, ReceiveErrorEventArgs receiveErrorEventArgs)
{
Debugger.Break();
}
static async Task GetContactPhone()
{
var offset = 0;
var keyboard = new ReplyKeyboardMarkup(new[]
{
new [] // last row
{
new KeyboardButton("shared your contact")
{
RequestContact = true
}
},
new [] // first row
{
new KeyboardButton("about usا"),
new KeyboardButton("contact usا"),
}
});
int count = 1;
while (true)
{
var Updates = await Bot.GetUpdates(offset);
foreach (var update in Updates)
{
Console.WriteLine("update "+ count.ToString());
count++;
var message = update.Message;
/* if (message == null || message.Type != MessageType.TextMessage)
{
return;
}
*/
//if (update.Type == UpdateType.MessageUpdate) { }
if(update.Type == UpdateType.MessageUpdate)
{
Console.WriteLine(update.Message.Chat.FirstName + " shared contact");
var cc = update.Message.Contact.PhoneNumber;
if (cc == null)
{
cc = "aa";
}
string ph = message.Contact.PhoneNumber;
await Bot.SendTextMessageAsync(update.Message.Chat.Id, cc, replyMarkup: keyboard);
//break;
}
else
{
if (message.Text == ("/start"))
{
Console.WriteLine(update.Message.Chat.FirstName + " start bot");
await Bot.SendTextMessageAsync(message.Chat.Id, "...abc... ",
replyMarkup: keyboard);
}
if (message.Text == ("about us"))
{
Console.WriteLine(update.Message.Chat.FirstName + " pressed 'about us'");
await Bot.SendTextMessageAsync(message.Chat.Id, "...def...",
replyMarkup: keyboard);
}
if (message.Text == "contact us"))
{
Console.WriteLine(update.Message.Chat.FirstName + "pressed 'contact us'");
await Bot.SendTextMessageAsync(message.Chat.Id, "...bbb...",
replyMarkup: keyboard);
}
}
offset = update.Id + 1;
}
}
}
}
}`
and this is result when I run it:
how can fix this problem?
I changed my first if statement condition to if (update.Type == UpdateType.MessageUpdate && message.Type != MessageType.TextMessage). now my bot wroking true.
@MrRoundRobin I can't get it in Update.Message.Contact. contact in null again.but when i use locationrequest by the same way is correct and i can get it by update.message.location
@erroe you need to make sure the message.type of the message you got is MessageType.ContactMessage
@mz1368 Take a look at this example:
hi I put keboarbutton for request_contact in my bot. I want to access to user's phone number after that he shared his contact. this is my same question and description in stackoverflow: http://stackoverflow.com/questions/38251447/how-can-i-save-users-phone-number-after-he-shared-his-contact how can I fix this problem?