Alexander Koryukin beni bu uygulamaya itti
Ve Kirill Kazakov'un VK gruplarından birinde yaptığı bir yorum:
Evet, hiç güvenli değil. Hesabımdan yalnızca aktivasyon komutlarını kabul eden bir telgraf botu yazmayı tercih ederim.
Böyle bir bot yazmaya karar verdim.
Yani, yapılacak ilk şey telegramda bir bot oluşturmaktır.
- Aramada @botfather adlı bir hesap bulduk.
- Ekranın altındaki Başlat düğmesine tıklayın
- Sonra ona / newbot komutunu yazıyoruz.
Sonra 2 basit soruyu cevaplıyoruz:
- İlk soru oluşturulacak botun adıdır. MyMikrotikRouter
- İkinci soru, oluşturulan botun takma adıdır (bot ile bitmelidir) MikrotikROuter_bot
Yanıt olarak, botumuzun jetonunu alacağız, benim durumumda:
HTTP API'sine erişmek için bu belirteci kullanın: 265373548:AAFyGCqJCei9mvcxvXOWBfnjSt1p3sX1XH4
Ardından, isme göre aramada botumuzu bulmanız gerekir. @MikrotikROuter_bot ve Başlat düğmesine basın.
Bundan sonra, tarayıcıyı açmanız ve aşağıdaki satırı girmeniz gerekir:
https://api.telegram.org/botXXXXXXXXXXXXXXXXXX/getUpdates
XXXXXXXXXXXXXXXXXXX, botunuzun simgesidir.
Aşağıdakine benzer bir sayfa açılacaktır:
Üzerinde şu metni buluyoruz:
"sohbet":{"id":631290,
Mikrotik için komut dosyası yazmak için gerekli tüm bilgilere sahibiz, yani:
Bot jetonu: 265373548:AAFyGCqJCei9mvcxvXOWBfnjSt1p3sX1XH4
Yazması gereken sohbet kimliği: 631290
Kontrol etmek için tarayıcıdan geçebiliriz:
https://api.telegram.org/bot265373548:AAFyGCqJCei9mvcxvXOWBfnjSt1p3sX1XH4/sendmessage?chat_id=631290&text=test
Sonuç almalı:
Size kolaylık sağlamak için bot için hemen komutlar ekleyeceğiz:
İsimli bir hesap bulma @botbaba
Sonra ona bir komut yazıyoruz. / setcommands
- hangi bot diye soracak
Biz yazarız:
@MikrotikROuter_bot
Komutları ekleyin:
- merhabaworld< — Sohbet 1'deki mesajı test edin
- onunçalışma-Sohbet 2'deki Test Mesajı
- wolmypc-bilgisayarımı uyandır
Şimdi, sohbete "/" yazarsanız şunları elde etmelisiniz:
Şimdi MikroTik'e geçelim.
RouterOS, dosyaları ftp veya http / https yoluyla kopyalamak için bir konsol yardımcı programına sahiptir, yardımcı programa fetch denir, kullanacağımız şey budur.
Açık terminal ve girin:
/tool fetch url="https://api.telegram.org/bot265373548:AAFyGCqJCei9mvcxvXOWBfnjSt1p3sX1XH4/sendmessage?chat_id=631290&text=test " keep-result=no
Lütfen MikroTik'in "» işaretten kaçmak «?' URL'sinde.
Sonuç almalı:
Şimdi komut dosyalarına geçelim:
Selam Dünya
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}
işe yarıyor
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"
Bot-token ve chat_id ile birlikte doğru mac ve arayüz adını belirtmeyi unutmayın.
Şimdi ne yaptıklarını biraz anlatacağım:
"Helloworld" betiği bir mesaj gönderir: "Merhaba dünya!" bot ile sohbetimize.
"itworking" betiği bir mesaj gönderir: "Test Tamam, Çalışıyor!" bot ile sohbetimize.
Bu betikler tanıtım amaçlıdır.
Olası uygulamalardan biri olarak "wolmypc" betiğini ekledim.
Komut dosyasının yürütülmesi üzerine bot, sohbete "wol OK" yazacaktır.
Aslında, kesinlikle herhangi bir komut dosyasını çalıştırabilirsiniz.
Bir görev oluşturun:
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
Okunabilir Görünümneden olduğu belli değil, ancak çalışan betikten küresel verileri ifşa etmiyor, betiği sistem önyüklendiğinde ekledi.
telgraf başlatma
: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") ;
Telegram
: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;
}
Bu nasıl çalışıyor
Her 30 saniyede bir "getUpdates" mesajlarımızı indirin, ardından öğrenmek için ayrıştırın güncelleme_id (mesaj numarası) ve metin (ekiplerimiz) ve chat_id . GetUpdates varsayılan olarak 1 ila 100 mesaj görüntüler, kolaylık sağlamak için komutu okuduktan sonra mesajı siliyoruz. Telegram api, bir mesajı okumak için + 1 mesaj numarasına ihtiyacınız olduğunu söylüyor
/getUpdates?offset=update_id + 1
Tamamı Mikrotik RB915 RouterOS 6.37.1 üzerinde test edilmiştir.
Aynı anda birçok komut gönderirseniz, hepsi 30 saniyelik aralıklarla sırayla yürütülür.
Not: Fikir için Kirill Kazakov'a ve senaryolarla ilgili yardım için arkadaşım Alexander'a çok teşekkürler.
referanslar
gnc:
03:11:16
Geliştirilmiş komut dosyaları:
chat_id için kontrol eklendi
Bir aptal olup olmadığını kontrol ederken, eğer biri botumuza yazarsa, ona cevap verecektir: "Seninle konuşamam. “, komutu tanımadığı takdirde benzer şekilde bize cevap verecektir.
Komutu yürüttükten sonra, bot sohbet aboneliğinden çıkar (wolmypc komut dosyasına bakın)
UPD
ile bulundu
UPD 08.12.2016
Görünüşe göre Telegram'da getUpdate'in "tükenmesini" biraz değiştirdiler. şimdi ana komut dosyasında mesaj ofsetini 2'den 1'e düzeltmeniz gerekiyor
değişiklikler
:local message [:pick $content ($startLoc + 2) $endLoc] ;
заменить на :
:local message [:pick $content ($startLoc + 1) $endLoc] ;
Kaynak: habr.com