isx*_*ker 11 c# telegram telegram-bot
我正在使用Telegram Bot API向用户发送即时消息.我已经安装了nuget包.电报开发人员推荐这个软件包.
我创建了一个电报机器人,并使用代码成功访问了它.当我向机器人发送消息时,机器人会获得有关发送者的一些信息.
我需要用户的电话号码在我们的系统中识别它们并将信息发回给他们.
我的问题是我可以获得用户电话号码telegramUserId吗?
我这样做是为了方便用户.如果我能得到用户电话号码,我不应该向用户索要.
现在我的命令是这样的:
debt 9811201243
Run Code Online (Sandbox Code Playgroud)
我想要
debt
Run Code Online (Sandbox Code Playgroud)
Ser*_*nov 12
不,遗憾的Telegram Bot API是不会返回电话号码.您应该使用Telegram API方法,或者明确地向用户询问.你也无法获得用户的"朋友".
您一定会检索以下信息:
useridfirst_name content (无论是什么:文字,照片等) date (unixtime)chat_id如果用户配置了它,你也会得到last_name和username.
Dan*_*sev 11
机器人2.0可以检查bot api docs.
https://core.telegram.org/bots/2-0-intro#locations-and-numbers https://core.telegram.org/bots/api#keyboardbutton
使用Telegram Bot API,只有当您向用户请求时,您才能获取电话号码,但用户不必写下该号码,他所要做的就是在对话中按一个按钮,号码就会发送到你。
当用户点击/myNumber
用户必须确认:
你会得到他的号码
这。是控制台输出:
看一下这个简单的控制台应用程序,但您需要做一些更改来处理数字:
将以Handler.ch下行添加到BotOnMessageReceived
if (message.Type == MessageType.Contact && message.Contact != null)
{
Console.WriteLine($"Phone number: {message.Contact.PhoneNumber}");
}
Run Code Online (Sandbox Code Playgroud)
这是存储库有一天被删除时所需的代码:
程序.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();
}
}
Run Code Online (Sandbox Code Playgroud)
处理程序.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;
}
}
Run Code Online (Sandbox Code Playgroud)