使用Telegram Bot API,您只有在向用户请求时才能获取电话号码,但用户不必写号码,他只需在对话中按一个按钮,号码就会发送到你。
![在此处输入图像描述](https://i.stack.imgur.com/TjBab.png)
当用户点击/myNumber
![在此处输入图像描述](https://i.stack.imgur.com/nxLOk.png)
用户必须确认:
![在此处输入图像描述](https://i.stack.imgur.com/1Tum5.png)
你会得到他的号码
![在此处输入图像描述](https://i.stack.imgur.com/Xq5yc.png)
这个。是控制台输出:
![在此处输入图像描述](https://i.stack.imgur.com/ZxB76.png)
看看这个简单的控制台应用程序,但您需要做一些更改来处理数字:
将以Handler.ch
下行添加到BotOnMessageReceived
if (message.Type == MessageType.Contact && message.Contact != null)
{
Console.WriteLine($"Phone number: {message.Contact.PhoneNumber}");
}
这是某天删除存储库时所需的代码:
程序.cs
public static class Program
{
private static TelegramBotClient? bot;
public static async Task Main()
{
bot = new TelegramBotClient(/*TODO: BotToken hier*/);
User me = await bot.GetMeAsync();
Console.Title = me.Username ?? "My awesome bot";
using var cts = new CancellationTokenSource();
ReceiverOptions receiverOptions = new() { AllowedUpdates = { } };
bot.StartReceiving(Handlers.HandleUpdateAsync,
Handlers.HandleErrorAsync,
receiverOptions,
cts.Token);
Console.WriteLine($"Start listening for @{me.Username}");
Console.ReadLine();
cts.Cancel();
}
}
处理程序.cs
internal class Handlers
{
public static Task HandleErrorAsync(ITelegramBotClient botClient, Exception exception, CancellationToken cancellationToken)
{
var errorMessage = exception switch
{
ApiRequestException apiRequestException => $"Telegram API Error:\n[{apiRequestException.ErrorCode}]\n{apiRequestException.Message}",
_ => exception.ToString()
};
Console.WriteLine(errorMessage);
return Task.CompletedTask;
}
public static async Task HandleUpdateAsync(ITelegramBotClient botClient, Update update, CancellationToken cancellationToken)
{
var handler = update.Type switch
{
UpdateType.Message => BotOnMessageReceived(botClient, update.Message!),
_ => UnknownUpdateHandlerAsync(botClient, update)
};
try
{
await handler;
}
catch (Exception exception)
{
await HandleErrorAsync(botClient, exception, cancellationToken);
}
}
private static async Task BotOnMessageReceived(ITelegramBotClient botClient, Message message)
{
Console.WriteLine($"Receive message type: {message.Type}");
if (message.Type == MessageType.Contact && message.Contact != null)
{
// TODO: save the number...
Console.WriteLine($"Phone number: {message.Contact.PhoneNumber}");
}
if (message.Type != MessageType.Text)
return;
var action = message.Text!.Split(' ')[0] switch
{
"/myNumber" => RequestContactAndLocation(botClient, message),
_ => Usage(botClient, message)
};
Message sentMessage = await action;
Console.WriteLine($"The message was sent with id: {sentMessage.MessageId}");
static async Task<Message> RequestContactAndLocation(ITelegramBotClient botClient, Message message)
{
ReplyKeyboardMarkup requestReplyKeyboard = new(
new[]
{
// KeyboardButton.WithRequestLocation("Location"), // this for the location if you need it
KeyboardButton.WithRequestContact("Send my phone Number"),
});
return await botClient.SendTextMessageAsync(chatId: message.Chat.Id,
text: "Could you please send your phone number?",
replyMarkup: requestReplyKeyboard);
}
static async Task<Message> Usage(ITelegramBotClient botClient, Message message)
{
const string usage = "/myNumber - to send your phone number";
return await botClient.SendTextMessageAsync(chatId: message.Chat.Id,
text: usage,
replyMarkup: new ReplyKeyboardRemove());
}
}
private static Task UnknownUpdateHandlerAsync(ITelegramBotClient botClient, Update update)
{
Console.WriteLine($"Unknown update type: {update.Type}");
return Task.CompletedTask;
}
}