bot4s / telegram

Telegram Bot API Wrapper for Scala
Apache License 2.0
418 stars 101 forks source link

Remove service message #58

Closed v-shaal closed 6 years ago

v-shaal commented 6 years ago

Can I remove the service message using the this for eg

xyz joined the group
pqr left the group 

I want to remove the telegram message whenever user join or leave the group

mukel commented 6 years ago

A bot can remove messages https://stackoverflow.com/a/45285278/5457433 from groups and receive leave/join notifications ... not sure about service messages. It shouldn't be hard to set it up e.g. a bot that deletes all messages. I'll give it a shot.

v-shaal commented 6 years ago

https://github.com/php-telegram-bot/core/issues/730 I am reffering to this , I am not sure about PHP though

mukel commented 6 years ago

Join/left messages are bundled in fields containing the users in Message. Please try this, couldn't test it, but should work or be very close.

import info.mukel.telegrambot4s.api._
import info.mukel.telegrambot4s.methods._
import info.mukel.telegrambot4s.models._

/**
  * Remove join/left messages from groups.
  */
object GroupBot extends TelegramBot
  with Polling {
  lazy val token = "TOKEN"

  override def receiveMessage(msg: Message): Unit = {
    super.receiveMessage(msg)
    if (msg.newChatMembers.isDefined || msg.leftChatMember.isDefined)
      request(DeleteMessage(msg.source, msg.messageId))
  }
}

GroupBot.run()
v-shaal commented 6 years ago

Thanks It worked , but only for single group ,when I added the bot in two group it tarted throwing me this error .

ERROR info.mukel.telegrambot4s.clients.AkkaClient - Telegram API exception
info.mukel.telegrambot4s.api.TelegramApiException: Bad Request: message can't be deleted
    at info.mukel.telegrambot4s.clients.AkkaClient.$anonfun$apply$3(AkkaClient.scala:56)
    at scala.concurrent.Future.$anonfun$flatMap$1(Future.scala:302)
    at scala.concurrent.impl.Promise.$anonfun$transformWith$1(Promise.scala:37)
    at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:60)
    at scala.concurrent.impl.ExecutionContextImpl$AdaptedForkJoinTask.exec(ExecutionContextImpl.scala:140)
    at java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:289)
    at java.util.concurrent.ForkJoinPool$WorkQueue.pollAndExecAll(ForkJoinPool.java:1021)
    at java.util.concurrent.ForkJoinPool$WorkQueue.execLocalTasks(ForkJoinPool.java:1046)
    at java.util.concurrent.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1058)
    at java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1692)
    at java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:157)
mukel commented 6 years ago

It seems like a permission issue. The bot must be admin or have permission to delete messages. https://core.telegram.org/bots/api#deletemessage

mukel commented 6 years ago

Any updates on this?

v-shaal commented 6 years ago

works perfect after giving permission.

sajadi1846 commented 4 years ago

hi . my bot delete the join and left service messages in my group but speed of deleting is so slow . when i add 50 person in one action with telegraph 50 service message apear in room and about 30 seconds for delete this 50 service messages with bot . my bot writen in php . see my code please . can you help me for solve this problem ? thanks

sajadi1846 commented 4 years ago

@$newmember = $result->message->new_chat_member->id; @$leftmember = $result->message->left_chat_member->id; @$gpid = $result->message->chat->id; @$messageid = $result->message->message_id; $telegram = new telegram(TOKEN,HOST,USERNAME,PASSWORD,DBNAME); if(!empty($newmember)|| !empty($leftmember)){ $telegram->deleteMessage($gpid,$messageid); die(); }