The hardware and bandwidth for this mirror is donated by dogado GmbH, the Webhosting and Full Service-Cloud Provider. Check out our Wordpress Tutorial.
If you wish to report a bug, or if you are interested in having us mirror your free-software or open-source project, please feel free to contact us at mirror[@]dogado.de.
This vignette describes advanced use cases for the filters used with
MessageHandler
or CommandHandler
.
When using MessageHandler
it is sometimes useful to have
more than one filter. This can be done using so called binary operators.
Using telegram.bot
, you can operate BaseFilter
instances with &
, |
and !
meaning AND, OR and NOT respectively.
<- MessageHandler(callback,
handler $video | MessageFilters$photo | MessageFilters$document
MessageFilters )
<- MessageHandler(callback,
handler $forwarded & MessageFilters$photo
MessageFilters )
<- MessageHandler(callback,
handler $photo & (!MessageFilters$forwarded)
MessageFilters )
It is also possible to write your own filters used with
MessageHandler
and CommandHandler
. In essence,
a filter is simply a function that receives a Message
instance and returns either TRUE
or FALSE
.
This function has to be implemented in a new class that inherits from
BaseFilter
, which allows it to be combined with other
filters. If a filter evaluates to TRUE
, the message will be
handled.
Say that for the kill
example we saw in the previous
page, we would like to filter that command so to make it accessible
only for a specific USER_ID
. Thereby, you could add a
filter:
<- function(message) message$from_user == "USER_ID" filter_user
You can make the function an instance of BaseFilter
either with its generator:
<- BaseFilter(filter = filter_user) filter_user
Or by coercing it with as.BaseFilter
:
<- as.BaseFilter(function(message) message$from_user == "USER_ID") filter_user
Remember that to make it work, your filter must be a
function
that takes a message
as input and
returns a boolean: TRUE
if the message should be handled,
FALSE
otherwise.
Now, you could update the handler with this filter:
<- CommandHandler("kill", kill, filter_user) kill_handler
Filters can also be added to the MessageFilters
object.
Within it, we can see that MessageFilters$text
and
MessageFilters$command
are mutually exclusive, so we could
add a filter for messages that can be either one of them. This would
result as:
$text_or_command <- BaseFilter(function(message) {
MessageFilters!is.null(message$text))
}
The class can of cause be named however you want, the only important things are:
BaseFilter
.filter
method.function
that takes a
message
as input and returns a boolean.The filter can then be used as:
<- MessageHandler(callback, MessageFilters$text_or_command) handler
These binaries (installable software) and packages are in development.
They may not be fully stable and should be used with caution. We make no claims about them.
Health stats visible at Monitor.