-
Notifications
You must be signed in to change notification settings - Fork 0
/
TelebotExtension.jar
103 lines (89 loc) · 3.46 KB
/
TelebotExtension.jar
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package com.rasitech.telebot;
import com.google.appinventor.components.annotations.*;
import com.google.appinventor.components.runtime.*;
import com.google.appinventor.components.runtime.util.*;
import com.google.appinventor.components.common.*;
import com.google.appinventor.components.runtime.errors.*;
import com.google.appinventor.components.runtime.collect.*;
import okhttp3.*;
import java.io.IOException;
import org.json.JSONException;
import org.json.JSONObject;
@DesignerComponent(
version = 1,
description = "Extension for Telegram Bot",
category = ComponentCategory.EXTENSION,
nonVisible = true,
iconName = "images/extension.png"
)
@SimpleObject(external = true)
public class TelebotExtension extends AndroidNonvisibleComponent {
private static final String BASE_URL = "https://api.telegram.org/bot";
private String botToken;
private OnMessageReceivedListener messageReceivedListener;
public TelebotExtension(ComponentContainer container) {
super(container.$form());
}
@SimpleProperty(category = PropertyCategory.BEHAVIOR)
public String BotToken() {
return botToken;
}
@DesignerProperty(editorType = PropertyTypeConstants.PROPERTY_TYPE_STRING,
defaultValue = "your_bot_token_here")
@SimpleProperty(description = "Get or set the bot token.")
public void BotToken(String token) {
botToken = token;
}
@SimpleEvent(description = "Event triggered when a message is received.")
public void OnMessageReceived(final String message) {
if (messageReceivedListener != null) {
form.runOnUiThread(new Runnable() {
@Override
public void run() {
messageReceivedListener.onMessageReceived(message);
}
});
}
}
@SimpleFunction(description = "Start listening for incoming messages.")
public void StartListening() {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(BASE_URL + botToken + "/getUpdates")
.build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
e.printStackTrace();
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if (response.isSuccessful()) {
String responseData = response.body().string();
parseAndTriggerEvent(responseData);
} else {
System.out.println("Error: " + response.code() + " " + response.message());
}
}
});
}
private void parseAndTriggerEvent(String responseData) {
try {
JSONObject jsonObject = new JSONObject(responseData);
if (jsonObject.has("result")) {
JSONObject resultObject = jsonObject.getJSONArray("result").optJSONObject(0);
if (resultObject != null && resultObject.has("message")) {
String message = resultObject.getJSONObject("message").toString();
OnMessageReceived(message);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
// Antarmuka berada di luar kelas
}
// Antarmuka OnMessageReceivedListener
interface OnMessageReceivedListener {
void onMessageReceived(String message);
}