Skip to content

Commit

Permalink
Initial version
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenlagus committed Mar 16, 2024
1 parent 0952b27 commit a70cad7
Show file tree
Hide file tree
Showing 20 changed files with 753 additions and 275 deletions.
4 changes: 4 additions & 0 deletions Writerside/topics/Lesson-1.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ import org.telegram.telegrambots.api.methods.send.SendMessage;
import org.telegram.telegrambots.api.objects.Update;
import org.telegram.telegrambots.longpolling.util.LongPollingSingleThreadUpdateConsumer;
import org.telegram.telegrambots.exceptions.TelegramApiException;
import org.telegram.telegrambots.meta.generics.TelegramClient;
import org.telegram.telegrambots.client.okhttp.OkHttpTelegramClient;

public class MyAmazingBot implements LongPollingSingleThreadUpdateConsumer {
private TelegramClient telegramClient = new OkHttpTelegramClient("YOUR_BOT_TOKEN");
Expand Down Expand Up @@ -164,6 +166,8 @@ import org.telegram.telegrambots.api.methods.send.SendMessage;
import org.telegram.telegrambots.api.objects.Update;
import org.telegram.telegrambots.longpolling.util.LongPollingSingleThreadUpdateConsumer;
import org.telegram.telegrambots.exceptions.TelegramApiException;
import org.telegram.telegrambots.meta.generics.TelegramClient;
import org.telegram.telegrambots.client.okhttp.OkHttpTelegramClient;

public class MyAmazingBot implements LongPollingSingleThreadUpdateConsumer {
private TelegramClient telegramClient = new OkHttpTelegramClient("YOUR_BOT_TOKEN");
Expand Down
6 changes: 6 additions & 0 deletions Writerside/topics/Lesson-2.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ import org.telegram.telegrambots.api.methods.send.SendMessage;
import org.telegram.telegrambots.api.objects.Update;
import org.telegram.telegrambots.longpolling.util.LongPollingSingleThreadUpdateConsumer;
import org.telegram.telegrambots.exceptions.TelegramApiException;
import org.telegram.telegrambots.meta.generics.TelegramClient;
import org.telegram.telegrambots.client.okhttp.OkHttpTelegramClient;

public class PhotoBot implements LongPollingSingleThreadUpdateConsumer {
private TelegramClient telegramClient = new OkHttpTelegramClient("YOUR_BOT_TOKEN");
Expand Down Expand Up @@ -297,12 +299,16 @@ import org.telegram.telegrambots.api.objects.replykeyboard.ReplyKeyboardRemove;
import org.telegram.telegrambots.api.objects.replykeyboard.buttons.KeyboardRow;
import org.telegram.telegrambots.longpolling.util.LongPollingSingleThreadUpdateConsumer;
import org.telegram.telegrambots.exceptions.TelegramApiException;
import org.telegram.telegrambots.meta.generics.TelegramClient;
import org.telegram.telegrambots.client.okhttp.OkHttpTelegramClient;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

public class PhotoBot implements LongPollingSingleThreadUpdateConsumer {
private TelegramClient telegramClient = new OkHttpTelegramClient("YOUR_BOT_TOKEN");

@Override
public void consume(Update update) {

Expand Down
2 changes: 2 additions & 0 deletions Writerside/topics/Lesson-3.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,8 @@ import org.telegram.telegrambots.api.methods.send.SendMessage;
import org.telegram.telegrambots.api.objects.Update;
import org.telegram.telegrambots.bots.TelegramLongPollingBot;
import org.telegram.telegrambots.exceptions.TelegramApiException;
import org.telegram.telegrambots.meta.generics.TelegramClient;
import org.telegram.telegrambots.client.okhttp.OkHttpTelegramClient;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
Expand Down
162 changes: 59 additions & 103 deletions Writerside/topics/Lesson-4.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,22 @@

# Lesson 4. Emoji

Welcome back! Now your know, how to log messages from users. But how to make bot's messages more user-friendly and beautiful? The answer is - [emoji](https://en.wikipedia.org/wiki/Emoji). I think you know what is emoji, so let's move forward.
Welcome back! Now your know, how to log messages from users. But how to make bot messages more user-friendly and beautiful? The answer is - [emoji](https://en.wikipedia.org/wiki/Emoji). I think you know what is emoji, so let's move forward.

Now, open `IntelliJ Idea` and create a new project. Create files `Main.java` and `EmojiTestBot.java` within the `src` directory. Here is first look of our files:

> `src/Main.java`
```java
import org.telegram.telegrambots.ApiContextInitializer;
import org.telegram.telegrambots.TelegramBotsApi;
import org.telegram.telegrambots.longpolling.TelegramBotsLongPollingApplication;
import org.telegram.telegrambots.exceptions.TelegramApiException;


public class Main {
public static void main(String[] args) {
// Initialize Api Context
ApiContextInitializer.init();

// Instantiate Telegram Bots API
TelegramBotsApi botsApi = new TelegramBotsApi();

// Register our bot
try {
botsApi.registerBot(new EmojiTestBot());
TelegramBotsLongPollingApplication botsApplication = new TelegramBotsLongPollingApplication();
botsApplication.registerBot(new EmojiTestBot());
} catch (TelegramApiException e) {
e.printStackTrace();
}
Expand All @@ -36,16 +30,20 @@ public class Main {
```java
import org.telegram.telegrambots.api.methods.send.SendMessage;
import org.telegram.telegrambots.api.objects.Update;
import org.telegram.telegrambots.bots.TelegramLongPollingBot;
import org.telegram.telegrambots.longpolling.util.LongPollingSingleThreadUpdateConsumer;
import org.telegram.telegrambots.exceptions.TelegramApiException;
import org.telegram.telegrambots.meta.generics.TelegramClient;
import org.telegram.telegrambots.client.okhttp.OkHttpTelegramClient;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class EmojiTestBot extends TelegramLongPollingBot {
public class EmojiTestBot implements LongPollingSingleThreadUpdateConsumer {
private TelegramClient telegramClient = new OkHttpTelegramClient("YOUR_BOT_TOKEN");

@Override
public void onUpdateReceived(Update update) {
public void consume(Update update) {

// We check if the update has a message and the message has text
if (update.hasMessage() && update.getMessage().hasText()) {
Expand All @@ -56,62 +54,46 @@ public class EmojiTestBot extends TelegramLongPollingBot {
String message_text = update.getMessage().getText();
long chat_id = update.getMessage().getChatId();
String answer = message_text;
SendMessage message = new SendMessage() // Create a message object object
.setChatId(chat_id)
.setText(answer);
log(user_first_name, user_last_name, Long.toString(user_id), message_text, answer);
SendMessage message = SendMessage // Create a message object object
.builder()
.chatId(chat_id)
.text(answer)
.build()
try {
execute(message); // Sending our message object to user
telegramClient.execute(message); // Sending our message object to user
} catch (TelegramApiException e) {
e.printStackTrace();
}
}
}



@Override
public String getBotUsername() {
// Return bot username
// If bot username is @MyAmazingBot, it must return 'MyAmazingBot'
return "EmojiTestBot";
}

@Override
public String getBotToken() {
// Return bot token from BotFather
return "12345:qwertyuiopASDGFHKMK";
}
private void log(String first_name, String last_name, String user_id, String txt, String bot_answer) {
System.out.println("\n ----------------------------");
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();
System.out.println(dateFormat.format(date));
System.out.println("Message from " + first_name + " " + last_name + ". (id = " + user_id + ") \n Text - " + txt);
System.out.println("Bot answer: \n Text - " + bot_answer);
}
}
```

Okey. Now lets install [emoji library](https://github.com/vdurmont/emoji-java):

* Via `Maven`:

```markup
<dependency>
<groupId>com.vdurmont</groupId>
<artifactId>emoji-java</artifactId>
<version>3.1.3</version>
</dependency>
```

* Via `Gradle`:

```text
compile 'com.vdurmont:emoji-java:3.1.3'
```

* Or just download `.jar` from [here](https://github.com/vdurmont/emoji-java/releases/download/v3.1.3/emoji-java-3.1.3.jar)
Ok. Now let's install [emoji library](https://github.com/vdurmont/emoji-java):

<tabs group="dependency">
<tab title="Maven" group-key="Maven">
<code-block lang="xml">
<![CDATA[
<dependency>
<groupId>com.vdurmont</groupId>
<artifactId>emoji-java</artifactId>
<version>%emoji_version%</version>
</dependency>
]]>
</code-block>
</tab>
<tab title="Gradle" group-key="Gradle">
<code-block lang="gradle">
<![CDATA[
compile 'com.vdurmont:emoji-java:%emoji_version%'
]]>
</code-block>
</tab>
<tab title="Manual Jar" group-key="Manual">
Download `.jar` from <a href="https://github.com/vdurmont/emoji-java/releases/download/v%emoji_version%/emoji-java-%emoji_version%.jar">here</a>
</tab>
</tabs>

Once library is installed, import it to your bot class:

Expand All @@ -132,21 +114,14 @@ Here is source code. You can also find it on [GitHub](https://github.com/Monster
> `src/Main.java`
```java
import org.telegram.telegrambots.ApiContextInitializer;
import org.telegram.telegrambots.TelegramBotsApi;
import org.telegram.telegrambots.longpolling.TelegramBotsLongPollingApplication;
import org.telegram.telegrambots.exceptions.TelegramApiException;

public class Main {
public static void main(String[] args) {
// Initialize Api Context
ApiContextInitializer.init();

// Instantiate Telegram Bots API
TelegramBotsApi botsApi = new TelegramBotsApi();

// Register our bot
try {
botsApi.registerBot(new EmojiTestBot());
TelegramBotsLongPollingApplication botsApplication = new TelegramBotsLongPollingApplication();
botsApplication.registerBot(new EmojiTestBot());
} catch (TelegramApiException e) {
e.printStackTrace();
}
Expand All @@ -158,19 +133,22 @@ public class Main {
> `src/EmojiTestBot.java`
```java
import com.vdurmont.emoji.EmojiParser;
import org.telegram.telegrambots.api.methods.send.SendMessage;
import org.telegram.telegrambots.api.objects.Update;
import org.telegram.telegrambots.bots.TelegramLongPollingBot;
import org.telegram.telegrambots.longpolling.util.LongPollingSingleThreadUpdateConsumer;
import org.telegram.telegrambots.exceptions.TelegramApiException;
import org.telegram.telegrambots.meta.generics.TelegramClient;
import org.telegram.telegrambots.client.okhttp.OkHttpTelegramClient;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class EmojiTestBot extends TelegramLongPollingBot {
private TelegramClient telegramClient = new OkHttpTelegramClient("YOUR_BOT_TOKEN");

@Override
public void onUpdateReceived(Update update) {
public void consume(Update update) {

// We check if the update has a message and the message has text
if (update.hasMessage() && update.getMessage().hasText()) {
Expand All @@ -181,46 +159,24 @@ public class EmojiTestBot extends TelegramLongPollingBot {
String message_text = update.getMessage().getText();
long chat_id = update.getMessage().getChatId();
String answer = EmojiParser.parseToUnicode("Here is a smile emoji: :smile:\n\n Here is alien emoji: :alien:");
SendMessage message = new SendMessage() // Create a message object object
.setChatId(chat_id)
.setText(answer);
log(user_first_name, user_last_name, Long.toString(user_id), message_text, answer);
SendMessage message = SendMessage // Create a message object object
.builder()
.chatId(chat_id)
.text(answer)
.build()
try {
execute(message); // Sending our message object to user
telegramClient.execute(message); // Sending our message object to user
} catch (TelegramApiException e) {
e.printStackTrace();
}
}
}



@Override
public String getBotUsername() {
// Return bot username
// If bot username is @MyAmazingBot, it must return 'MyAmazingBot'
return "EmojiTestBot";
}

@Override
public String getBotToken() {
// Return bot token from BotFather
return "12345:qwertyuiopASDGFHKMK";
}
private void log(String first_name, String last_name, String user_id, String txt, String bot_answer) {
System.out.println("\n ----------------------------");
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();
System.out.println(dateFormat.format(date));
System.out.println("Message from " + first_name + " " + last_name + ". (id = " + user_id + ") \n Text - " + txt);
System.out.println("Bot answer: \n Text - " + bot_answer);
}
}
```

Now you can see our beautiful messages:

![Bot sends messages with emoji](https://github.com/MonsterDeveloper/java-telegram-bot-tutorial/raw/master/media/Bot_emoji.png)
![Bot sends messages with emoji](Bot_emoji.png)

Our lesson came to an end. Thank you for reading this. See you soon!

28 changes: 10 additions & 18 deletions Writerside/topics/Lesson-5.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
# Lesson 5. Deploy your bot

I think, when you are reading this, you have created your own bot with the help from my book. So now, its time to run it not on your home computer ~~with Intel Pentium II~~, but on professional server hardware. I will show how to deploy your bot on [DigitalOcean hosting](https://m.do.co/c/1a3a7fad419f).
I think, when you are reading this, you have created your own bot with the help from my book. So now, it's time to run it not on your home computer ~~with Intel Pentium II~~, but on professional server hardware. I will show how to deploy your bot on [DigitalOcean hosting](https://m.do.co/c/1a3a7fad419f).

## Creating droplet

Firstly, you need to create account on DigitalOcean. Open [this link](https://m.do.co/c/1a3a7fad419f) to get 10$ as gift from me, enter your email and password and click "Create an account"

![Register](https://github.com/MonsterDeveloper/java-telegram-bot-tutorial/raw/master/media/do_register.png)
![Register](do_register.png)

Then, follow register insctructions. Once you are in your control panel, create a new droplet.
Then, follow register instructions. Once you are in your control panel, create a new droplet.

Select OS. I recommend using Ubuntu 16.04.01 x64. Then choose preffered plan. For Java bot, you can select 512-1GB RAM \(its enough for start\). Select datacenter's region \(I recommend to choose nearest city\), scroll down and click "Create". Check your email inbox for letter like this:
Select OS. I recommend using Ubuntu 16.04.01 x64. Then choose preferred plan. For Java bot, you can select 512-1GB RAM \(its enough for start\). Select datacenter's region \(I recommend to choose nearest city\), scroll down and click "Create". Check your email inbox for letter like this:

![Droplet settings email](https://github.com/MonsterDeveloper/java-telegram-bot-tutorial/raw/master/media/do_email.png)
![Droplet settings email](do_email.png)

## Connecting via SSH

Expand All @@ -23,27 +23,19 @@ You will need next software for that:

When you install it, open PuTTY and write server IP and port \(default 22\).

![PuTTY login](https://github.com/MonsterDeveloper/java-telegram-bot-tutorial/raw/master/media/do_pytty.png)
![PuTTY login](do_pytty.png)

And click "Open". You will see something like:

![PuTTY Security Alert](https://github.com/MonsterDeveloper/java-telegram-bot-tutorial/raw/master/media/do_fingerprint.png)
![PuTTY Security Alert](do_fingerprint.png)

Click "Yes". Then login as "root" user and with password that you have recieved via email. Now we need to install Java on your server. Type following:
Click "Yes". Then login as "root" user and with password that you have received via email.

```bash
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install default-jre
sudo apt-get install default-jdk
sudo add-apt-repository ppa:webupd8team/java
sudo apt-get update
sudo apt-get install oracle-java8-installer
```
Now we need to install Java on your server. You can follow this [guide](https://www.digitalocean.com/community/tutorials/how-to-install-java-with-apt-on-ubuntu-22-04)

Type `java -version` to check installation. You will see something like that:

![Java Version](https://github.com/MonsterDeveloper/java-telegram-bot-tutorial/raw/master/media/java_version.png)
![Java Version](java_version.png)

## Creating and uploading JAR

Expand Down
Loading

0 comments on commit a70cad7

Please sign in to comment.