Whakahohenga mamao o nga tuhinga Mikrotik mai i Telegram

Na Alexander Koryukin ahau i akiaki ki tenei whakatinanatanga GeXoGeN me tana whakaputangaTe huri mamao i te rorohiko mo te kore utu, kaore he SMS me te kore kapua, ma te whakamahi i te Mikrotik".

Me tetahi korero i tetahi o nga roopu VK na Kirill Kazakov:

Ae, kaore rawa i te haumaru. He pai ake taku tuhi i te karetao waea ka whakaae noa ki nga whakahau whakahohe mai i taku putea.

I whakatau ahau ki te tuhi i taua bot.

Na, ko te mahi tuatahi ko te hanga karetao ki te waea waea.

  • Ka kitea e matou i roto i te rapu he kaute ko @botfather
  • Patohia te paatene Tīmata kei raro o te mata
  • Na ka tuhia e matou ki a ia te whakahau / newbot

Na ka whakautua e matou nga patai ngawari e rua:

  • Ko te patai tuatahi ko te ingoa o te bot ka hangaia. TakuMikrotikROuter
  • Ko te patai tuarua ko te ingoa ingoa o te karetao e hangaia ana (me mutu ki te bot) MikrotikROuter_bot

Hei whakautu, ka whiwhi tatou i te tohu o to tatou bot, i taku keehi ko:

Whakamahia tenei tohu hei uru ki te API HTTP: 265373548:AAFyGCqJCei9mvcxvXOWBfnjSt1p3sX1XH4

Whakahohenga mamao o nga tuhinga Mikrotik mai i Telegram
Na, me rapu e koe to maatau bot i roto i te rapu ma te ingoa @MikrotikROuter_bot ka pēhi i te pātene Tīmata.

I muri i tera, me whakatuwhera koe i te kaitirotiro ka uru ki te raina e whai ake nei:

 https://api.telegram.org/botXXXXXXXXXXXXXXXXXX/getUpdates

Ko XXXXXXXXXXXXXXXXXX te tohu o to bot.

Ka tuwhera he wharangi rite ki enei e whai ake nei:

Whakahohenga mamao o nga tuhinga Mikrotik mai i Telegram

Ka kitea e matou te tuhinga e whai ake nei i runga:

"kōrerorero":{"id":631290,

Na, kei a matou nga korero e tika ana mo te tuhi tuhinga mo Mikrotik, ara:

Tohu Bot: 265373548:AAFyGCqJCei9mvcxvXOWBfnjSt1p3sX1XH4

Kōrerorero ID me tuhi ia: 631290

Hei tirotiro, ka taea e taatau te haere ma te tirotiro:

https://api.telegram.org/bot265373548:AAFyGCqJCei9mvcxvXOWBfnjSt1p3sX1XH4/sendmessage?chat_id=631290&text=test

Me whiwhi te hua:

Whakahohenga mamao o nga tuhinga Mikrotik mai i Telegram

Mo to maatau, ka taapiri tonu matou i nga whakahau mo te karetao:

Te kimi kaute me te ingoa @papa
Na ka tuhia e matou he whakahau ki a ia / setcommands

  • Ka patai ia ko tehea bot

Ka tuhi matou:
@MikrotikROuter_bot

Tāpirihia ngā tono:

  • helloworld< — Karere whakamatautau i te korerorero 1
  • itsworking-Karere Whakamatau i runga i te korerorero 2
  • wolmypc-ara taku PC

Inaianei ki te pato koe "/" i roto i te korerorero, me whiwhi koe:

Whakahohenga mamao o nga tuhinga Mikrotik mai i Telegram

Na, kia neke atu ki te MikroTik.

Kei a RouterOS tetahi taputapu papatohu mo te kape i nga konae ma te ftp, http / https ranei, ka kiia te taputapu ko te tiki, koinei te mea ka whakamahia e matou.

Tuwhera tauranga ka tomo:

/tool fetch url="https://api.telegram.org/bot265373548:AAFyGCqJCei9mvcxvXOWBfnjSt1p3sX1XH4/sendmessage?chat_id=631290&text=test " keep-result=no

Kia mahara kei te hiahia a MikroTik ""ki te mawhiti i te tohu"?'i roto i te URL.

Me whiwhi te hua:

Whakahohenga mamao o nga tuhinga Mikrotik mai i Telegram

Inaianei me neke atu ki nga tuhinga:

Tuhinga o mua

system script add name="helloworld" policy=read source={/tool fetch url="https://api.telegram.org/bot265373548:AAFyGCqJCei9mvcxvXOWBfnjSt1p3sX1XH4/sendmessage?chat_id=631290&text=Hello,world! " keep-result=no}

tana mahi

system script add name="itsworking" policy=read source={/tool fetch url="https://api.telegram.org/bot265373548:AAFyGCqJCei9mvcxvXOWBfnjSt1p3sX1XH4/sendmessage?chat_id=631290&text=Test OK, it's Working " keep-result=no}

wolmypc

system script add name="wolmypc" policy=read source="/tool wol mac=XX:XX:XX:XX:XX:XX interface=ifnamer
    n/tool fetch url="https://api.telegram.org/boXXXXXXXXXXXXXXXXXXX?chat_id=631290&text=wol OK" keep-resul
    t=no"

Kaua e wareware ki te tohu i te mac tika me te ingoa atanga, tae atu ki te bot-token me te chat_id.

Inaianei ka whakamarama au i a raatau mahi:

Ka tukuna e te tuhinga "helloworld" he karere: "Kia ora, e te ao!" ki ta maatau korero me te bot.
Ka tukuna e te tuhinga "itsworking" he karere: "Whakamatau OK, kei te Mahi!" ki ta maatau korero me te bot.
Ko enei tuhinga mo nga kaupapa whakaatu.
I tapiritia e ahau te tuhinga "wolmypc" hei tetahi o nga whakatinanatanga ka taea.
Ka mahia te tuhinga, ka tuhia e te karetao "Wol OK" ki te korerorero.
Inaa, ka taea e koe te whakahaere i nga tuhinga katoa.

Waihangahia he mahi:

Telegram.src

/system scheduler
add interval=30s name=Telegram on-event=":tool fetch url=("https://api.telegr
    am.org/".$botID."/getUpdates") ;r
    n:global content [/file get [/file find name=getUpdates] contents] ;r
    n:global startLoc 0;r
    n:global endLoc 0;r
    nr
    n:if ( [/file get [/file find name=getUpdates] size] > 50 ) do={r
    nr
    n:set startLoc  [:find $content "update_id" $lastEnd ] ;r
    n:set startLoc ( $startLoc + 11 ) ;r
    n:local endLoc [:find $content "," $startLoc] ;r
    n:local messageId ([:pick $content $startLoc $endLoc] + (1));r
    n:put [$messageId] ;r
    n:#log info message="updateID $messageId" ;r
    nr
    n:set startLoc  [:find $content "text" $lastEnd ] ;r
    n:set startLoc ( $startLoc  + 7 ) ;r
    n:local endLoc [:find $content "," ($startLoc)] ;r
    n:set endLoc ( $endLoc - 1 ) ;r
    n:local message [:pick $content ($startLoc + 2) $endLoc] ;r
    n:put [$message] ;r
    n:#log info message="message $message ";r
    nr
    n:set startLoc  [:find $content "chat" $lastEnd ] ;r
    n:set startLoc ( $startLoc + 12 ) ;r
    n:local endLoc [:find $content "," $startLoc] ;r
    n:local chatId ([:pick $content $startLoc $endLoc]);r
    n:put [$chatId] ;r
    n:#log info message="chatID $chatId ";r
    nr
    n:if (($chatId = $myChatID) and (:put [/system script find name=$messa
    ge] != "")) do={r
    n:system script run $message} else={:tool fetch url=("https://api.teleg
    ram.org/".$botID."/sendmessage?chat_id=".$chatId."&text=I can't t
    alk with you. ") keep-result=no} ;r
    n:tool fetch url=("https://api.telegram.org/".$botID."/getUpdates?
    offset=$messageId") keep-result=no; r
    n} r
    n" policy=
    ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon 
    start-date=nov/02/2010 start-time=00:00:00
	
add name=Telegram-startup on-event=":delay 5r
    n:global botID "botXXXXXXXXXXXXXXXXXX" ;r
    n:global myChatID "631290" ;r
    n:global startLoc 0;r
    n:global endLoc 0;r
    n:tool fetch url=("https://api.telegram.org/".$botID."/getUpdates") 
    ;" policy=
    ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon 
    start-time=startup

Tirohanga Panuikaore i te maarama he aha, engari mai i te tuhinga mahi kaore i te whakaatu i nga raraunga o te ao, ka taapirihia te tuhinga ka peke te punaha.
Whakaoho Telegram

:delay 5
:global botID "botXXXXXXXXXXXXXXXXXX" ;   token bot
:global myChatID "xxxxxx" ;                               chat_id
:global startLoc 0;
:global endLoc 0;
:tool fetch url=("https://api.telegram.org/".$botID."/getUpdates") ;

waea

:tool fetch url=("https://api.telegram.org/".$botID."/getUpdates") ;
:global content [/file get [/file find name=getUpdates] contents] ;
:global startLoc 0;
:global endLoc 0;

:if ( [/file get [/file find name=getUpdates] size] > 50 ) do={

:set startLoc  [:find $content "update_id" $lastEnd ] ;
:set startLoc ( $startLoc + 11 ) ;
:local endLoc [:find $content "," $startLoc] ;
:local messageId ([:pick $content $startLoc $endLoc] + (1));
:put [$messageId] ;
#:log info message="updateID $messageId" ;

:set startLoc  [:find $content "text" $lastEnd ] ;
:set startLoc ( $startLoc  + 7 ) ;
:local endLoc [:find $content "," ($startLoc)] ;
:set endLoc ( $endLoc - 1 ) ;
:local message [:pick $content ($startLoc + 2) $endLoc] ;
:put [$message] ;
#:log info message="message $message ";

:set startLoc  [:find $content "chat" $lastEnd ] ;
:set startLoc ( $startLoc + 12 ) ;
:local endLoc [:find $content "," $startLoc] ;
:local chatId ([:pick $content $startLoc $endLoc]);
:put [$chatId] ;
#:log info message="chatID $chatId ";

:if (($chatId = $myChatID) and (:put [/system script find name=$message] != "")) do={
:system script run $message} else={:tool fetch url=("https://api.telegram.org/".$botID."/sendmessage?chat_id=".$chatId."&text=I can't talk with you. ") keep-result=no} ;
:tool fetch url=("https://api.telegram.org/".$botID."/getUpdates?offset=$messageId") keep-result=no; 
} 

Nahea teie ohipa

Tangohia a maatau karere "getUpdates" ia 30 hēkona, katahi ka poroporoaki kia mohio ai whakahou_id (tau karere) me kuputuhi (a matou kapa) me kōrero_id . Ma te taunoa, ka whakaatuhia e getUpdates mai i te 1 ki te 100 nga karere, mo te waatea, i muri i te panui i te whakahau, ka mukua e matou te karere. E kii ana a Telegram api ki te panui i tetahi karere ka hiahia koe ki te nama karere + 1

/getUpdates?offset=update_id + 1

I whakamatauria katoa i runga i Mikrotik rb915 RouterOS 6.37.1
Mena ka tukuna e koe nga tono maha i te wa kotahi, ka mahia katoatia me te 30 hēkona te roa.

PS Ka nui te mihi ki a Kirill Kazakov mo te whakaaro me taku hoa Alexander mo te awhina i nga tuhinga.

tohutoro

habrahabr.ru/post/313794
1spla.ru/index.php/blog/telegram_bot_for_mikrotik
core.telegram.org/bots/api
wiki.mikrotik.com/wiki/Manual:Te tuhi tuhi

whakahōunga:

03:11:16

Nga tuhinga kua pai ake:

Kua taapirihia te haki mo te chat_id
Ma te tirotiro mo te wairangi, ki te tuhi tetahi ki to maatau bot, ka whakautu ia ki a ia: "Kaore au e korero ki a koe. ", ka whakahoki mai ano ki a maatau ki te kore e mohio ki te whakahau.
Whai muri i te whakahaerenga o te whakahau, ka wete te karetao ki te korerorero (tirohia te tuhinga wolmypc)

DUP

I kitea me 7Taiwhakaheke7 Ko te konae kei runga ake nei ~14 nga karere kua kore e tukahia e te tono rapu (whakawhāititanga Mikrotik). No reira, a muri ake nei, ka huri au i te tuhinga ki te lua, mihi 7Taiwhakaheke7 mo tenei, kare au i mohio mo lua.

UPD 08.12.2016/XNUMX/XNUMX

i roto i te Telegram, te ahua nei, he paku kee ratou i te "poha" o te getUpdate. Inaianei kei roto i te tuhinga matua me whakatika e koe te panui i te 2 ki te 1

huringa

:local message [:pick $content ($startLoc + 2) $endLoc] ;

заменить на :

:local message [:pick $content ($startLoc + 1) $endLoc] ;

Source: will.com