首页 > 解决方案 > 向特定号码的电话发送短信

问题描述

我得到了App api_id,App api_hashProduction configurationfrom telegram.org, 我需要使用 from 这个方法messages.sendMessage来发送短信到特定号码电话的电报(例如:+1888888)。我如何使用这种方法。有没有简单的样本?

标签: androidtelegram

解决方案


我建议您使用 MTProto 上的顶层库来使事情变得更容易。例如,您可以使用Telethon。您应该使用SendMessageRequest来发送消息。创建客户端后,您可以这样调用它(在最新版本的 Telethon 中,电话号码会自动解析):

from telethon.tl.functions.messages import SendMessageRequest
client(SendMessageRequest('phone_number', 'hello'))

如果您使用的是 TDLib,则可以使用此功能(取自此处)或类似功能:

private static void sendMessage(long chatId, String message) {
    // initialize reply markup just for testing
    TdApi.InlineKeyboardButton[] row = {new TdApi.InlineKeyboardButton("https://telegram.org?1", new TdApi.InlineKeyboardButtonTypeUrl()), new TdApi.InlineKeyboardButton("https://telegram.org?2", new TdApi.InlineKeyboardButtonTypeUrl()), new TdApi.InlineKeyboardButton("https://telegram.org?3", new TdApi.InlineKeyboardButtonTypeUrl())};
    TdApi.ReplyMarkup replyMarkup = new TdApi.ReplyMarkupInlineKeyboard(new TdApi.InlineKeyboardButton[][]{row, row, row});

    TdApi.InputMessageContent content = new TdApi.InputMessageText(new TdApi.FormattedText(message, null), false, true);
    client.send(new TdApi.SendMessage(chatId, 0, false, false, replyMarkup, content), defaultHandler);
}

不要忘记,您需要先将每个电话号码添加到用户的 Telegram 联系人中才能获得chatId. 可以通过将电话号码数组传递给此函数来实现:

---functions---
contacts.importContacts#2c800be5 contacts:Vector<InputContact> = contacts.ImportedContacts

推荐阅读