├── .gitignore ├── LICENSE ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── com │ │ └── mikers │ │ └── botserver │ │ ├── BackgroundService.kt │ │ ├── BotServiceJava.java │ │ └── MainActivity.kt │ └── res │ ├── drawable-v24 │ └── ic_launcher_foreground.xml │ ├── drawable │ └── ic_launcher_background.xml │ ├── layout │ └── activity_main.xml │ ├── mipmap-anydpi-v26 │ ├── ic_launcher.xml │ └── ic_launcher_round.xml │ ├── mipmap-hdpi │ ├── ic_launcher.webp │ └── ic_launcher_round.webp │ ├── mipmap-mdpi │ ├── ic_launcher.webp │ └── ic_launcher_round.webp │ ├── mipmap-xhdpi │ ├── ic_launcher.webp │ └── ic_launcher_round.webp │ ├── mipmap-xxhdpi │ ├── ic_launcher.webp │ └── ic_launcher_round.webp │ ├── mipmap-xxxhdpi │ ├── ic_launcher.webp │ └── ic_launcher_round.webp │ ├── values-night │ └── themes.xml │ ├── values │ ├── colors.xml │ ├── strings.xml │ └── themes.xml │ └── xml │ ├── backup_rules.xml │ └── data_extraction_rules.xml ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── local.properties └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled class file 2 | *.class 3 | 4 | # Log file 5 | *.log 6 | 7 | # BlueJ files 8 | *.ctxt 9 | 10 | # Mobile Tools for Java (J2ME) 11 | .mtj.tmp/ 12 | 13 | # Package Files # 14 | *.jar 15 | *.war 16 | *.nar 17 | *.ear 18 | *.zip 19 | *.tar.gz 20 | *.rar 21 | 22 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 23 | hs_err_pid* 24 | .gradle 25 | .idea 26 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 AngryApple 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # botserver 2 | Sample for public performance 3 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | id 'com.android.application' 3 | id 'org.jetbrains.kotlin.android' 4 | } 5 | 6 | android { 7 | compileSdk 32 8 | 9 | defaultConfig { 10 | applicationId "com.mikers.botserver" 11 | minSdk 26 12 | targetSdk 33 13 | versionCode 1 14 | versionName "1.0" 15 | 16 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 17 | } 18 | 19 | buildTypes { 20 | release { 21 | minifyEnabled false 22 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' 23 | } 24 | } 25 | compileOptions { 26 | sourceCompatibility JavaVersion.VERSION_1_8 27 | targetCompatibility JavaVersion.VERSION_1_8 28 | } 29 | kotlinOptions { 30 | jvmTarget = '1.8' 31 | } 32 | } 33 | 34 | dependencies { 35 | implementation 'androidx.core:core-ktx:1.8.0' 36 | implementation 'androidx.appcompat:appcompat:1.4.2' 37 | implementation 'com.google.android.material:material:1.6.1' 38 | implementation 'androidx.constraintlayout:constraintlayout:2.1.4' 39 | 40 | // telegram api 41 | implementation 'com.github.pengrad:java-telegram-bot-api:6.0.1' 42 | } -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | 19 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /app/src/main/java/com/mikers/botserver/BackgroundService.kt: -------------------------------------------------------------------------------- 1 | package com.mikers.botserver 2 | 3 | import android.app.Notification 4 | import android.app.NotificationChannel 5 | import android.app.NotificationManager 6 | import android.app.Service 7 | import android.content.Intent 8 | import android.os.IBinder 9 | import com.mikers.botserver.ServiceState.ACTION_START 10 | 11 | object ServiceState { 12 | const val ACTION_START = "ACTION_START" 13 | const val ACTION_STOP = "ACTION_STOP" 14 | } 15 | 16 | interface IBroadcastSender { 17 | fun sendBroadcastFunc(intent: Intent) 18 | } 19 | 20 | class BackgroundService : Service(), IBroadcastSender { 21 | 22 | private val CHANNEL_ID = "opencoffeebot606" 23 | private val CHANNEL_NAME = "opencoffee" 24 | private val CHANNEL_DESCRIPTION = "Test" 25 | 26 | override fun onBind(intent: Intent): IBinder? { 27 | return null 28 | } 29 | 30 | override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int { 31 | when (intent.action) { 32 | ACTION_START -> { 33 | addNotification() 34 | startService() 35 | } 36 | ServiceState.ACTION_STOP -> stopForegroundService() 37 | } 38 | return START_STICKY 39 | } 40 | 41 | override fun sendBroadcastFunc(intent: Intent) { 42 | sendBroadcast(intent) 43 | } 44 | 45 | private fun addNotification() { 46 | val channel = NotificationChannel( 47 | CHANNEL_ID, 48 | CHANNEL_NAME, 49 | NotificationManager.IMPORTANCE_DEFAULT 50 | ) 51 | channel.description = CHANNEL_DESCRIPTION 52 | 53 | getSystemService(NotificationManager::class.java).createNotificationChannel(channel) 54 | 55 | val notification = Notification.Builder(this, CHANNEL_ID) 56 | .setSmallIcon(R.drawable.ic_launcher_foreground) 57 | .setContentTitle("Bot working!") 58 | .setContentText("Bot working, all message reading!") 59 | startForeground(1001, notification.build()) 60 | } 61 | 62 | private fun startService() { 63 | BotServiceJava.getInstance().start(this) 64 | } 65 | 66 | private fun stopForegroundService() { 67 | BotServiceJava.getInstance().stop() 68 | // Stop foreground service and remove the notification. 69 | stopForeground(true) 70 | // Stop the foreground service. 71 | stopSelf() 72 | } 73 | 74 | } -------------------------------------------------------------------------------- /app/src/main/java/com/mikers/botserver/BotServiceJava.java: -------------------------------------------------------------------------------- 1 | package com.mikers.botserver; 2 | 3 | import android.content.Intent; 4 | 5 | import com.pengrad.telegrambot.TelegramBot; 6 | import com.pengrad.telegrambot.UpdatesListener; 7 | import com.pengrad.telegrambot.model.Message; 8 | import com.pengrad.telegrambot.request.SendMessage; 9 | 10 | import java.text.DateFormat; 11 | import java.text.SimpleDateFormat; 12 | import java.util.Date; 13 | import java.util.TimeZone; 14 | 15 | public class BotServiceJava { 16 | 17 | private static BotServiceJava instance; 18 | private final TelegramBot bot = new TelegramBot("botToken"); 19 | 20 | public static BotServiceJava getInstance() { 21 | if(instance == null) { 22 | instance = new BotServiceJava(); 23 | } 24 | return instance; 25 | } 26 | 27 | public void start(IBroadcastSender broadcastSender) { 28 | bot.setUpdatesListener(updates -> { 29 | try { 30 | Message message = updates.get(0).message(); 31 | Long chatId = message.chat().id(); 32 | String text = message.text(); 33 | 34 | // send broadcast message 35 | Intent intent = new Intent("com.mikers.botserver.DATA"); 36 | intent.putExtra("text", getMessageTime(message.date()) + ": " + text); 37 | broadcastSender.sendBroadcastFunc(intent); 38 | 39 | bot.execute(new SendMessage(chatId, "Ваш текст: " + text)); 40 | 41 | return UpdatesListener.CONFIRMED_UPDATES_ALL; 42 | } catch (Exception ex) { 43 | return UpdatesListener.CONFIRMED_UPDATES_ALL; 44 | } 45 | }); 46 | } 47 | 48 | public void stop() { 49 | bot.removeGetUpdatesListener(); 50 | } 51 | 52 | private String getMessageTime(Integer messageTime) { 53 | Date date = new Date(messageTime * 1000); 54 | DateFormat formatter = new SimpleDateFormat("HH:mm:ss"); 55 | formatter.setTimeZone(TimeZone.getTimeZone("UTC")); 56 | return formatter.format(date); 57 | } 58 | 59 | } -------------------------------------------------------------------------------- /app/src/main/java/com/mikers/botserver/MainActivity.kt: -------------------------------------------------------------------------------- 1 | package com.mikers.botserver 2 | 3 | import android.annotation.SuppressLint 4 | import android.app.ActivityManager 5 | import android.content.BroadcastReceiver 6 | import android.content.Context 7 | import android.content.Intent 8 | import android.content.IntentFilter 9 | import androidx.appcompat.app.AppCompatActivity 10 | import android.os.Bundle 11 | import android.widget.Button 12 | import android.widget.TextView 13 | 14 | class MainActivity : AppCompatActivity() { 15 | 16 | private lateinit var broadcastReceiver: MainActivity.DataBroadcastReceiver 17 | private lateinit var outputTextView: TextView 18 | private lateinit var textView: TextView 19 | private lateinit var enableButton: Button 20 | 21 | private companion object { 22 | const val ACTION_DATA = "com.mikers.botserver.DATA" 23 | val filters = arrayOf(ACTION_DATA) 24 | val intentFilter: IntentFilter by lazy { 25 | IntentFilter().apply { 26 | filters.forEach { addAction(it) } 27 | } 28 | } 29 | } 30 | 31 | @SuppressLint("SetTextI18n") 32 | override fun onCreate(savedInstanceState: Bundle?) { 33 | super.onCreate(savedInstanceState) 34 | setContentView(R.layout.activity_main) 35 | 36 | broadcastReceiver = DataBroadcastReceiver() 37 | applicationContext.registerReceiver(broadcastReceiver, intentFilter) 38 | 39 | textView = findViewById(R.id.textView) 40 | enableButton = findViewById(R.id.enableButton) 41 | outputTextView = findViewById(R.id.outputTextView) 42 | 43 | enableButton.setOnClickListener { 44 | val intent = Intent(this, BackgroundService::class.java) 45 | if (!isMyServiceRunning(BackgroundService::class.java)) { 46 | setEnabled() 47 | intent.action = ServiceState.ACTION_START 48 | } else { 49 | setDisabled() 50 | intent.action = ServiceState.ACTION_STOP 51 | } 52 | startService(intent) 53 | } 54 | 55 | if (isMyServiceRunning(BackgroundService::class.java)) { 56 | setEnabled() 57 | } else { 58 | setDisabled() 59 | } 60 | } 61 | 62 | private fun setEnabled() { 63 | textView.text = "Service running" 64 | enableButton.text = "Stop" 65 | } 66 | 67 | private fun setDisabled() { 68 | textView.text = "Service stopped" 69 | enableButton.text = "Start" 70 | } 71 | 72 | private fun isMyServiceRunning(serviceClass: Class<*>): Boolean { 73 | val manager = getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager 74 | for (service in manager.getRunningServices(Int.MAX_VALUE)) { 75 | if (serviceClass.name == service.service.className) { 76 | return true 77 | } 78 | } 79 | return false 80 | } 81 | 82 | @SuppressLint("SetTextI18n") 83 | private fun showData(intent: Intent) { 84 | outputTextView.text = "${intent.getStringExtra("text")}\n${outputTextView.text}" 85 | } 86 | 87 | inner class DataBroadcastReceiver : BroadcastReceiver() { 88 | override fun onReceive(context: Context, intent: Intent) { 89 | when (intent.action) { 90 | ACTION_DATA -> showData(intent) 91 | } 92 | } 93 | } 94 | 95 | } -------------------------------------------------------------------------------- /app/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 8 | 9 | 15 | 18 | 21 | 22 | 23 | 24 | 30 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_launcher_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 10 | 15 | 20 | 25 | 30 | 35 | 40 | 45 | 50 | 55 | 60 | 65 | 70 | 75 | 80 | 85 | 90 | 95 | 100 | 105 | 110 | 115 | 120 | 125 | 130 | 135 | 140 | 145 | 150 | 155 | 160 | 165 | 170 | 171 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | 19 | 20 |