rubenlagus / TelegramBots

Java library to create bots using Telegram Bots API
https://telegram.me/JavaBotsApi
MIT License
4.77k stars 1.22k forks source link

how send message to mybot #181

Closed mbarzegar93 closed 7 years ago

mbarzegar93 commented 7 years ago

hi i create a bot using https://github.com/rubenlagus/TelegramBots/wiki/Getting-Started code is exactly like wiki the only difference is i use my own bot user name and token; but when i run it nothing happen i want to know how to send a text message to mybot and users receive it; i create a myfirstbot class that extends TelegramLongPollingBot and a main class like tutorial in wiki; now should i create a object of myfirstbot in main class and call onUpdateReceived and pass it and update obejct? if i should do this how can i send a text?

Clevero commented 7 years ago

If I understood you correctly:

Just run your bot with your main class and open a new chat in Telegram with your bot. For example, if I created a bot with the username clevero_myfirstbot, I could just open a new chat with the following URL https://telegram.me/clevero_myfirstbot or searching for @clevero_myfirstbot in the search bar of one of the official Telegram clients

Happy hacking :)

mbarzegar93 commented 7 years ago

i already start a chat with my bot in telegram,but there is no command! i want test that my robot work so how i could send a message to robot so users can see the message in their chat! i mean how write a code so it send the message "hello" to bot users what is the code for doing that?

mbarzegar93 commented 7 years ago

this is my code for myfirstbot class

` import org.telegram.telegrambots.ApiContextInitializer; import org.telegram.telegrambots.TelegramBotsApi; import org.telegram.telegrambots.exceptions.TelegramApiException; public class myfirstbot extends TelegramLongPollingBot{ @Override public void onUpdateReceived(Update update) { if (update.hasMessage()&&update.getMessage().hasText()){ SendMessage message = new SendMessage() .setChatId(update.getMessage().getChatId()) .setText(update.getMessage().getText()); try{ sendMessage(message); }catch(TelegramApiException e){ e.printStackTrace(); } } }

@Override
public String getBotUsername(){
    return "bot username";
}

@Override
public String getBotToken(){
    return "my token";
}

} `

and this is my main class code:

` import org.telegram.telegrambots.ApiContextInitializer; import org.telegram.telegrambots.TelegramBotsApi; import org.telegram.telegrambots.exceptions.TelegramApiException;

public class Main{ public static void main(String[] args){

    ApiContextInitializer.init();
    TelegramBotsApi botsApi = new TelegramBotsApi();
    try{
        botsApi.registerBot(new myfirstbot());
    }catch(TelegramApiException e){
        e.printStackTrace();
    }

}

} ` now what code i should add to these code to send "hello" to all users in bot in telegram

dkder3k commented 7 years ago

When your bot gets text message from user, in method "onUpdateReceived" will be created instance of SendMessage class. In your code it is "message". There is method "setText" for this class, where you can set text as parameter. Further this message will be sent to user in trycatch with "sendMessage" method. It works something like this, as I know.

mbarzegar93 commented 7 years ago

tnx dkder3k but i know how set a specific message for when bot get a message and send it to user i want to know how to send a message to all user in bot without they send any message;

dkder3k commented 7 years ago

I think "setChatId" method will help. Get chatID, remember it, later set it to your message and send it.

mbarzegar93 commented 7 years ago

first ,i think chatID use for specific user not all users,i mean if i get a chatID it will be for specific person and if i use it the message will send to that specific person not all users in bot second person should send a message then i can have the chatID so it not useful for me,i want send a message to all user in bot like channel that you type a message and everyone see it

rubenlagus commented 7 years ago

There is no way to get all the users id that your bot knows, you should persist them from incoming updates if you need them.

In any case, you can always create a channel and make your bot post messages there using channel username as chat_id