【问题标题】:Sends a text message to a specific number phone向特定号码的电话发送短信
【发布时间】:2019-03-05 05:10:31
【问题描述】:

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

【问题讨论】:

    标签: android telegram


    【解决方案1】:

    我建议您使用 MTProto 之上的顶层库来简化操作。例如,您可以使用Telethon。您应该使用SendMessageRequest 来发送消息。在creating a client 之后你可以这样调用它(在最新版本的 Telethon 中,电话号码会自动解析):

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

    如果您使用的是 TDLib,则可以使用此函数(取自 here)或类似函数:

    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
    

    【讨论】:

    • 我的代码是java,我可以在我的项目中使用telethon吗?
    • 直接,没有。但是您可以使用ProcessBuilder 来运行您的python 脚本。见stackoverflow.com/questions/3468987/…
    • 我接受你的回答,但如果你有一个在 java androi 中使用 telethon 的链接简单示例,请获取 me.plaese.thanks 很多
    • 如果你想在android中使用它,你最好看看这里的官方TDLib:github.com/tdlib/td
    • 另请看 TDLib 的 Java 示例:github.com/tdlib/td/tree/master/example/java
    猜你喜欢
    • 2023-04-02
    • 1970-01-01
    • 2013-12-04
    • 1970-01-01
    • 2015-01-04
    • 1970-01-01
    • 2019-01-09
    • 2014-02-16
    相关资源
    最近更新 更多