Skip to content

Instantly share code, notes, and snippets.

@VioletXF
Last active July 20, 2020 14:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save VioletXF/b77e6de4721a164d7ed83601e70d3cae to your computer and use it in GitHub Desktop.
Save VioletXF/b77e6de4721a164d7ed83601e70d3cae to your computer and use it in GitHub Desktop.
importPackage(android.content);
importPackage(android.app);
importClass(android.widget.Toast);
importClass(android.graphics.drawable.Icon);
const CHANNEL_ID = "call_owner";
const ignoreActionName = "msgbot.call_owner.ignore";//그냥 시스템 전반적으로 유니크하게 하시면 됩니다.
const replyActionName="msgbot.call_owner.reply";
const replyKey="key_reply";
const bot = BotManager.getCurrentBot();
let repliers={};
let receiver = new BroadcastReceiver({
onReceive: function(ctx, intent) {
let action = intent.getAction();
let roomName=intent.getStringExtra("roomName");
try{
if (action.equals(ignoreActionName)) {
ctx.getSystemService(Context.NOTIFICATION_SERVICE).cancel(java.lang.String(roomName).hashCode());
} else if(action.equals(replyActionName)){
let content = RemoteInput.getResultsFromIntent(intent).getCharSequence(replyKey).toString();
repliers[intent.getStringExtra("roomName")].reply(content);
ctx.getSystemService(Context.NOTIFICATION_SERVICE).cancel(java.lang.String(roomName).hashCode());
}
} catch (e){
Log.e(e,true);
}
}});
let filter = new IntentFilter(ignoreActionName);
filter.addAction(replyActionName);
App.getContext().registerReceiver(receiver, filter);
function createNotificationChannel() {
let name = "호출벨";
let importance = NotificationManager.IMPORTANCE_DEFAULT;
let channel = NotificationChannel(CHANNEL_ID, name, importance);
let manager = App.getContext().getSystemService(Context.NOTIFICATION_SERVICE);
manager.createNotificationChannel(channel);
}
createNotificationChannel();
function notify(msg, title, content) {
let riBuilder= new RemoteInput.Builder(replyKey);
riBuilder.setLabel("답장");
let remoteInput = riBuilder.build();
repliers[msg.room]=msg;
let replyIntent=new Intent(replyActionName);
replyIntent.putExtra("roomName", msg.room);
let replyPIntent=PendingIntent.getBroadcast(App.getContext(),
java.lang.String(msg.room).hashCode(),
replyIntent,
PendingIntent.FLAG_UPDATE_CURRENT
);
let replyAction= new Notification.Action.Builder(0,
"답장", replyPIntent)
.addRemoteInput(remoteInput)
.build()
let intentAction = new Intent(ignoreActionName);
intentAction.putExtra("roomName",msg.room);
let pendingIntent = PendingIntent.getBroadcast(App.getContext(), 1, intentAction, PendingIntent.FLAG_UPDATE_CURRENT);
let icon = Icon.createWithBitmap(msg.author.avatar.getBitmap());
let builder = Notification.Builder(App.getContext(), CHANNEL_ID)
.setSmallIcon(icon)
.setLargeIcon(icon)
.setContentTitle(title)
.setContentText(content)
.setPriority(Notification.PRIORITY_DEFAULT)
.addAction(0, "무시", pendingIntent)
.addAction(replyAction)
App.getContext().getSystemService(Context.NOTIFICATION_SERVICE).notify(java.lang.String(msg.room).hashCode(), builder.build());
}
function onCommand(msg) {
switch (msg.command) {
case "호출벨":
let text=msg.args.join(" ");
notify(msg, msg.room+"에서 "+msg.author.name+"님이 호출함", text);
break;
}
}
function onStartCompile() {
App.getContext().unregisterReceiver(receiver);
}
bot.addListener(Event.START_COMPILE, onStartCompile);
bot.addListener(Event.COMMAND, onCommand);
bot.setCommandPrefix("@");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment