I got App api_id
, App api_hash
and Production configuration
from telegram.org
, I need to use from this method messages.sendMessage
for Sends a text message to a specific number phone's telegram(for example: +1888888). How can I use from this method. Is there any a simple sample?
Asked
Active
Viewed 1,587 times
3

itsmysterybox
- 2,748
- 3
- 21
- 26

jo jo
- 1,758
- 3
- 20
- 34
1 Answers
3
I suggest you using a top-layer library over MTProto to make things easier. For example you can use Telethon. You should use SendMessageRequest in order to send message. After creating a client you can call it like this (in newest version of Telethon the phone number is resolved automatically):
from telethon.tl.functions.messages import SendMessageRequest
client(SendMessageRequest('phone_number', 'hello'))
If you're using TDLib, you may use this function (taken from here) or a similar one:
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);
}
Don't forget that, you need to add each phone number to user's Telegram contacts first to get the chatId
. It can be achieved by passing an array of phone numbers to this function:
---functions---
contacts.importContacts#2c800be5 contacts:Vector<InputContact> = contacts.ImportedContacts

Ali Hashemi
- 3,158
- 3
- 34
- 48
-
My code is java,can i use from telethon in my project? – jo jo Sep 29 '18 at 17:00
-
Directly, no. However you can use `ProcessBuilder` to run your python script. See https://stackoverflow.com/questions/3468987/executing-another-application-from-java – Ali Hashemi Sep 29 '18 at 17:48
-
I accept your answer but if you have a link simple sample for use telethon in java androi get me.plaese.thanks a lot – jo jo Sep 29 '18 at 17:50
-
if you want to use it in android, you'd better look at the official TDLib here: https://github.com/tdlib/td – Ali Hashemi Sep 29 '18 at 17:52
-
Also look at Java examples of TDLib: https://github.com/tdlib/td/tree/master/example/java – Ali Hashemi Sep 29 '18 at 17:53
-
I just need to send message from my app to telegram contact list phone with this library.Thank's a lot – jo jo Sep 29 '18 at 17:56
-
I can't use from TDLib.Get me this error: By not providing "FindTd.cmake" in CMAKE_MODULE_PATH this project has asked CMake to find a package configuration file provided by "Td", but CMake did not find one. – jo jo Sep 30 '18 at 12:05
-
And I can't install C++14 compatible compiler. – jo jo Sep 30 '18 at 12:06