Alexander Koryukin ผลักดันให้ฉันดำเนินการนี้
และความคิดเห็นในกลุ่ม VK โดย Kirill Kazakov:
ใช่ มันไม่ปลอดภัยเลย ฉันอยากจะเขียนบอทโทรเลขที่ยอมรับเฉพาะคำสั่งเปิดใช้งานจากบัญชีของฉัน
ฉันตัดสินใจที่จะเขียนบอทดังกล่าว
สิ่งแรกที่ต้องทำคือสร้างบอทในโทรเลข
- เราพบในการค้นหาบัญชีชื่อ @botfather
- คลิกที่ปุ่มเริ่มที่ด้านล่างของหน้าจอ
- จากนั้นเราเขียนคำสั่ง / newbot ให้เขา
จากนั้นเราตอบคำถามง่ายๆ 2 ข้อ:
- คำถามแรกคือชื่อของบอทที่จะสร้าง MyMikrotikRouter
- คำถามที่สองคือชื่อเล่นของบอทที่สร้างขึ้น (ควรลงท้ายด้วยบอท) MikrotikROuter_bot
ในการตอบกลับ เราจะได้รับโทเค็นของบอทของเรา ในกรณีของฉันคือ:
ใช้โทเค็นนี้เพื่อเข้าถึง HTTP API: 265373548:AAFyGCqJCei9mvcxvXOWBfnjSt1p3sX1XH4
จากนั้นคุณต้องค้นหาบอทของเราในการค้นหาโดยใช้ชื่อ @MikrotikRouter_bot และกดปุ่มเริ่ม
หลังจากนั้น คุณต้องเปิดเบราว์เซอร์และป้อนบรรทัดต่อไปนี้:
https://api.telegram.org/botXXXXXXXXXXXXXXXXXX/getUpdates
โดยที่ XXXXXXXXXXXXXXXXXX คือโทเค็นของบอทของคุณ
หน้าที่คล้ายกับต่อไปนี้จะเปิดขึ้น:
เราพบข้อความต่อไปนี้:
"แชท":{"รหัส":631290,
ดังนั้นเราจึงมีข้อมูลที่จำเป็นทั้งหมดสำหรับการเขียนสคริปต์สำหรับ Mikrotik ได้แก่ :
โทเค็นบอท: 265373548:AAFyGCqJCei9mvcxvXOWBfnjSt1p3sX1XH4
Chat ID ที่เขาควรเขียน: 631290
ในการตรวจสอบ เราสามารถผ่านเบราว์เซอร์:
https://api.telegram.org/bot265373548:AAFyGCqJCei9mvcxvXOWBfnjSt1p3sX1XH4/sendmessage?chat_id=631290&text=test
ควรได้รับผลลัพธ์:
เพื่อความสะดวก เราจะเพิ่มคำสั่งสำหรับบอททันที:
ค้นหาบัญชีที่มีชื่อ @บอทฟาเธอร์
จากนั้นเราก็เขียนคำสั่งให้เขา / setcommands
- เขาจะถามว่าบอทไหน
พวกเราเขียน:
@MikrotikRouter_bot
เพิ่มคำสั่ง:
- helloworld< — ทดสอบข้อความในแชท 1
- มันทำงาน-ทดสอบข้อความในการแชท 2
- wolmypc-ปลุกพีซีของฉัน
ตอนนี้ถ้าคุณพิมพ์ "/" ในการแชท คุณควรได้รับ:
ทีนี้มาต่อที่ MikroTik กัน
RouterOS มียูทิลิตี้คอนโซลสำหรับการคัดลอกไฟล์ผ่าน ftp หรือ http / https ยูทิลิตี้นี้เรียกว่า fetch ซึ่งเป็นสิ่งที่เราจะใช้
เปิด สถานีปลายทาง และป้อน:
/tool fetch url="https://api.telegram.org/bot265373548:AAFyGCqJCei9mvcxvXOWBfnjSt1p3sX1XH4/sendmessage?chat_id=631290&text=test " keep-result=no
โปรดทราบว่า MikroTik ต้องการ "» หลบป้าย «?' ใน URL
ควรได้รับผลลัพธ์:
ตอนนี้ไปที่สคริปต์:
สวัสดีชาวโลก
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}
มันทำงาน
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}
โวลมีพีซี
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"
อย่าลืมระบุ mac และชื่ออินเทอร์เฟซที่ถูกต้อง รวมถึง bot-token และ chat_id
ตอนนี้ฉันจะอธิบายเล็กน้อยเกี่ยวกับสิ่งที่พวกเขาทำ:
สคริปต์ "helloworld" ส่งข้อความ: "Hello, world!" ในการแชทของเรากับบอท
สคริปต์ "itsworking" ส่งข้อความ: "Test OK, it's Working!" ในการแชทของเรากับบอท
สคริปต์เหล่านี้มีวัตถุประสงค์เพื่อการสาธิต
ฉันเพิ่มสคริปต์ "wolmypc" เป็นหนึ่งในการใช้งานที่เป็นไปได้
เมื่อดำเนินการตามสคริปต์ บอทจะเขียนว่า "wol OK" ในการแชท
อันที่จริง คุณสามารถเรียกใช้สคริปต์ใดก็ได้
สร้างงาน:
โทรเลข.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
มุมมองที่อ่านได้ไม่ชัดเจนว่าทำไม แต่จากสคริปต์การทำงาน มันไม่เปิดเผยข้อมูลส่วนกลาง เพิ่มสคริปต์เมื่อระบบบูท
การเริ่มต้นโทรเลข
: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;
}
Какэтоработает
ดาวน์โหลดข้อความ "getUpdates" ของเราทุกๆ 30 วินาที จากนั้นแยกวิเคราะห์เพื่อค้นหา อัพเดต_id (หมายเลขข้อความ) และ ข้อความ (ทีมงานของเรา) และ แชท_ไอดี . ตามค่าเริ่มต้น getUpdates จะแสดงตั้งแต่ 1 ถึง 100 ข้อความ เพื่อความสะดวก หลังจากอ่านคำสั่ง เราจะลบข้อความนั้น Telegram api บอกว่าจะอ่านข้อความคุณต้องมีหมายเลขข้อความ + 1
/getUpdates?offset=update_id + 1
ทั้งหมดทดสอบบน Mikrotik rb915 RouterOS 6.37.1
หากคุณส่งคำสั่งหลายคำสั่งพร้อมกัน คำสั่งทั้งหมดจะถูกดำเนินการตามช่วงเวลา 30 วินาที
ป.ล. ขอขอบคุณ Kirill Kazakov สำหรับแนวคิดและ Alexander เพื่อนของฉันสำหรับความช่วยเหลือเกี่ยวกับสคริปต์
การอ้างอิง
ปรับปรุง:
03:11:16
สคริปต์ที่ได้รับการปรับปรุง:
เพิ่มการตรวจสอบสำหรับ chat_id
ตรวจสอบคนโง่ถ้ามีคนเขียนถึงบอทของเรา เขาจะตอบเขาว่า: "ฉันคุยกับคุณไม่ได้ “ จะตอบเราในทำนองเดียวกันหากไม่รู้จักคำสั่ง
หลังจากดำเนินการคำสั่ง บอทจะยกเลิกการสมัครแชท (ดูสคริปต์ wolmypc)
DUP
พบกับ
UPD 08.12.2016/XNUMX/XNUMX
เห็นได้ชัดว่าใน Telegram พวกเขาเปลี่ยน "ไอเสีย" ของ getUpdate เล็กน้อย ตอนนี้ในสคริปต์หลักคุณต้องแก้ไขข้อความชดเชยจาก 2 เป็น 1
การเปลี่ยนแปลง
:local message [:pick $content ($startLoc + 2) $endLoc] ;
заменить на :
:local message [:pick $content ($startLoc + 1) $endLoc] ;
ที่มา: will.com