TelegramBots / Telegram.Bot

.NET Client for Telegram Bot API
https://telegrambots.github.io/book
MIT License
3.22k stars 690 forks source link

get user's phone number after he shared his contact with bot in private chat #198

Closed erroe closed 8 years ago

erroe commented 8 years ago

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?

MrRoundRobin commented 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

erroe commented 8 years ago

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?

erroe commented 8 years ago

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: error

how can fix this problem?

erroe commented 8 years ago

I changed my first if statement condition to if (update.Type == UpdateType.MessageUpdate && message.Type != MessageType.TextMessage). now my bot wroking true.

mz1368 commented 7 years ago

@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

Olfi01 commented 7 years ago

@erroe you need to make sure the message.type of the message you got is MessageType.ContactMessage

poulad commented 7 years ago

@mz1368 Take a look at this example:

https://github.com/TelegramBots/telegram.bot/blob/more-tests/test/Telegram.Bot.Tests.Integ/SendingMessages/MessageReplyMarkupTests.cs#L34-L51