Delphi Send Message To Telegram instead of Line Notify Part 2 Delphi Send API

เอาละๆ มาถึงตอนที่สองที่ต้องใช้ Delphi ในการส่งก่อนอื่นเราต้องมารู้จักกับ
Rest Debuger ของ Delphi กันก่อน เจ้านี้คือ Tool ที่เอาไว้ทดสอบยิง API จาก Delphi แต่ความเจ๋งของมันคือ เมื่อเราทำการทดสอบเราสามารถ Copy Component มาวางใน Form ได้เลยไม่ต้องไปไล่ Properties เพื่อกำหนดค่าใหม่แต่อย่างใด โดยเราสามารถเรียกใช้ได้จาก เมนู Tools → REST Debugger

เราจะได้หน้าตาเหมือนรูปด้านล่าง ให้เรานำ URL ที่เหมือนกับที่เราใส่ใน Postman

Method เป็น GET ส่วน URL เป็น

https://api.telegram.org/botYOUR_BOT_API_TOKEN/sendMessage

ตรง YOUR_BOT_API_TOKEN ใส่ Token ที่ได้มา อย่าลบคำว่า bot ละ

ในส่วน Content-Type เลือก เป็น x-www-form-urlencoded

ในส่วนของ Parameters ให้กด Add แล้วใส่ตามรูป
โดยมี Param 2 ตัวคือ chat_id กับ text โดย
Kind เป็น Body
Name เป็น ชื่อ Param
Value เป็น ค่าที่ใส่
เลือก Do not encode แล้วกด Apply แล้วทดสอบโดยการกดปุ่ม Send Request

หากสำเร็จจะได้ผลลัพธ์แบบในรูปด้านล่าง

กดปุ่ม Copy Components เพื่อ Copy การตั้งค่าที่เราใช้ทดสอบไปกำหนดค่าใน Components

นำไปกดวางใน form ก็จะได้หน้าตาประมาณนี้

กำหนดหน้าตาของ form โดยใส่ช่องสำหรับกำหนด chat_id และ Text แล้วที่สำคัญปุ่มส่ง Request ให้ BOT ส่งเข้าช่องแชต

ใส่ code เข้าไปเพื่อให้รับค่าจาก Textbox ไปส่ง API
RESTRequest1.Params.Items[0].Value := TxtChatID.Text;
RESTRequest1.Params.Items[1].Value := TxtMessage.Text;
RESTRequest1.Execute;

เมื่อ Run โปรแกรมแล้วกด Send ก็จะมีข้อความขึ้นที่ Telegram

เป็นไงครับไม่ยากเนอะ เท่านี้ก็เป็นอันเรียร้อย

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

No responses yet

Write a response

More from Piyanat Nimkhuntod