การสร้างบอทโทรเลขตัวแรกของคุณ: คำแนะนำทีละขั้นตอน
เผยแพร่แล้ว: 2022-03-11แชทบอทมักถูกขนานนามว่าเป็นการปฏิวัติวิธีที่ผู้ใช้โต้ตอบกับเทคโนโลยีและธุรกิจ พวกเขามีอินเทอร์เฟซที่ค่อนข้างเรียบง่ายเมื่อเทียบกับแอปทั่วไป เนื่องจากพวกเขาต้องการเพียงผู้ใช้ในการแชท และแชทบอทควรจะเข้าใจและทำทุกอย่างที่ผู้ใช้ต้องการจากพวกเขา อย่างน้อยก็ในทางทฤษฎี
หลายอุตสาหกรรมกำลังเปลี่ยนการบริการลูกค้าเป็นระบบแชทบอท นั่นเป็นเพราะต้นทุนที่ลดลงอย่างมากเมื่อเทียบกับมนุษย์จริง และเนื่องจากความแข็งแกร่งและความพร้อมใช้งานอย่างต่อเนื่อง Chatbots มอบการสนับสนุนผู้ใช้ในระดับหนึ่งโดยไม่มีค่าใช้จ่ายเพิ่มเติมมากนัก
ปัจจุบันแชทบอทถูกใช้ในหลายสถานการณ์ ตั้งแต่งานง่ายๆ เช่น การแสดงข้อมูลเวลาและสภาพอากาศ ไปจนถึงการดำเนินการที่ซับซ้อนมากขึ้น เช่น การวินิจฉัยทางการแพทย์เบื้องต้นและการสื่อสาร/การสนับสนุนลูกค้า คุณสามารถสร้างแชทบอทที่จะช่วยลูกค้าของคุณเมื่อพวกเขาถามคำถามบางอย่างเกี่ยวกับผลิตภัณฑ์ของคุณ หรือคุณสามารถสร้างแชทบ็อตผู้ช่วยส่วนตัวที่สามารถจัดการงานพื้นฐานและเตือนคุณเมื่อถึงเวลาต้องไปประชุมหรือไปยิม
มีตัวเลือกมากมายเมื่อพูดถึงจุดที่คุณสามารถปรับใช้แชทบ็อตของคุณได้ และหนึ่งในการใช้งานที่พบบ่อยที่สุดคือแพลตฟอร์มโซเชียลมีเดีย เนื่องจากคนส่วนใหญ่ใช้พวกเขาเป็นประจำ แอปส่งข้อความโต้ตอบแบบทันทีสามารถพูดได้เช่นเดียวกัน แม้ว่าจะมีข้อแม้บางประการ
Telegram เป็นหนึ่งในแพลตฟอร์ม IM ที่ได้รับความนิยมมากขึ้นในปัจจุบัน เนื่องจากทำให้คุณสามารถจัดเก็บข้อความบนคลาวด์แทนที่จะเป็นเพียงอุปกรณ์ของคุณ และรองรับหลายแพลตฟอร์มได้ดี เนื่องจากคุณสามารถมี Telegram บน Android, iOS, Windows และเกือบ แพลตฟอร์มอื่นใดที่สามารถรองรับเวอร์ชันเว็บได้ การสร้างแชทบ็อตบนโทรเลขนั้นค่อนข้างง่ายและต้องใช้ขั้นตอนไม่กี่ขั้นตอนซึ่งใช้เวลาเพียงเล็กน้อยในการดำเนินการให้เสร็จ แชทบอทสามารถรวมเข้ากับกลุ่มและช่องทางของโทรเลขได้ และยังทำงานด้วยตัวเองอีกด้วย
ในบทช่วยสอนนี้ เราจะสร้างบอทโทรเลขซึ่งให้ภาพอวาตาร์จากอวาตาร์ที่น่ารักแก่คุณ ตัวอย่างของเราจะเกี่ยวข้องกับการสร้างบอทโดยใช้ Flask และปรับใช้บนเซิร์ฟเวอร์ Heroku ฟรี
เพื่อให้บทช่วยสอนนี้สมบูรณ์ คุณจะต้องติดตั้ง Python 3 ในระบบของคุณ เช่นเดียวกับทักษะในการเขียนโค้ด Python นอกจากนี้ ความเข้าใจที่ดีเกี่ยวกับวิธีการทำงานของแอปจะเป็นส่วนเสริมที่ดี แต่ไม่จำเป็น เนื่องจากเราจะพูดถึงเนื้อหาส่วนใหญ่ที่เรานำเสนออย่างละเอียด คุณต้องติดตั้ง Git ในระบบของคุณด้วย
แน่นอน บทช่วยสอนนี้ต้องใช้บัญชีโทรเลขด้วย ซึ่งให้บริการฟรี คุณสามารถลงทะเบียนได้ที่นี่ จำเป็นต้องมีบัญชี Heroku ด้วย และคุณสามารถรับได้ฟรีที่นี่
ทำให้บอทโทรเลขของคุณมีชีวิต
ในการสร้างแชทบ็อตบน Telegram คุณต้องติดต่อ BotFather ซึ่งโดยพื้นฐานแล้วคือบอทที่ใช้สร้างบอทอื่นๆ
คำสั่งที่คุณต้องการคือ /newbot
ซึ่งนำไปสู่ขั้นตอนต่อไปนี้เพื่อสร้างบอทของคุณ:
บอทของคุณควรมีสองแอตทริบิวต์: ชื่อและชื่อผู้ใช้ ชื่อจะแสดงขึ้นสำหรับบอทของคุณ ในขณะที่ชื่อผู้ใช้จะถูกใช้สำหรับกล่าวถึงและแชร์
หลังจากเลือกชื่อบอทและชื่อผู้ใช้ของคุณแล้ว—ซึ่งต้องลงท้ายด้วย “บอท”—คุณจะได้รับข้อความที่มีโทเค็นการเข้าถึงของคุณ และคุณจะต้องบันทึกโทเค็นเพื่อการเข้าถึงและชื่อผู้ใช้ไว้อย่างชัดเจนในภายหลัง เนื่องจากคุณจะต้องใช้
รหัสลอจิก Chatbot
เราจะใช้ Ubuntu ในบทช่วยสอนนี้ สำหรับผู้ใช้ Windows คำสั่งส่วนใหญ่ที่นี่จะทำงานโดยไม่มีปัญหาใดๆ แต่หากคุณประสบปัญหาใดๆ กับการตั้งค่าสภาพแวดล้อมเสมือน โปรดอ่านลิงก์นี้ สำหรับผู้ใช้ Mac บทช่วยสอนนี้น่าจะใช้ได้ดี
ขั้นแรก มาสร้างสภาพแวดล้อมเสมือนจริงกัน ช่วยแยกข้อกำหนดของโครงการออกจากสภาพแวดล้อม Python ทั่วโลก
$ python -m venv botenv/
ตอนนี้เราจะมี botenv/
ซึ่งจะมีไลบรารี Python ทั้งหมดที่เราจะใช้ ไปข้างหน้าและเปิดใช้งาน virtualenv
โดยใช้คำสั่งต่อไปนี้:
$ source botenv/bin/activate
ห้องสมุดที่เราต้องการสำหรับบอทของเราคือ:
- Flask: ไมโครเฟรมเวิร์กเว็บที่สร้างขึ้นใน Python
- Python-telegram-bot: เสื้อคลุมโทรเลขใน Python
- คำขอ: ไลบรารี Python http ยอดนิยม
คุณสามารถติดตั้งได้ในสภาพแวดล้อมเสมือนโดยใช้คำสั่ง pip ดังนี้:
(telebot) $ pip install flask (telebot) $ pip install python-telegram-bot (telebot) $ pip install requests
ตอนนี้ มาดูไดเร็กทอรีโครงการของเรากัน
. ├── app.py ├── telebot │ ├── credentials.py │ | . │ | you can build your engine here │ | . │ └── __init__.py └── botenv
ในไฟล์ credentials.py
เราจะต้องมีสามตัวแปร:
bot_token = "here goes your access token from BotFather" bot_user_name = "the username you entered" URL = "the heroku app link that we will create later"
ตอนนี้กลับไปที่ app.py ของเราและทำตามรหัสทีละขั้นตอน:
# import everything from flask import Flask, request import telegram from telebot.credentials import bot_token, bot_user_name,URL
global bot global TOKEN TOKEN = bot_token bot = telegram.Bot(token=TOKEN)
ตอนนี้เรามีออบเจ็กต์บอทซึ่งจะใช้สำหรับการดำเนินการใด ๆ ที่เราต้องการให้บอทดำเนินการ
# start the flask app app = Flask(__name__)
เรายังต้องผูกฟังก์ชันกับเส้นทางเฉพาะ กล่าวอีกนัยหนึ่ง เราต้องบอก Flask ว่าต้องทำอย่างไรเมื่อมีการเรียกที่อยู่เฉพาะ ข้อมูลรายละเอียดเพิ่มเติมเกี่ยวกับขวดและเส้นทางสามารถพบได้ที่นี่
ในตัวอย่างของเรา ฟังก์ชันเส้นทางจะตอบสนองต่อ URL ซึ่งโดยพื้นฐานแล้วคือ /{token}
และนี่คือ URL ที่ Telegram จะเรียกเพื่อรับการตอบกลับสำหรับข้อความที่ส่งไปยังบอท
@app.route('/{}'.format(TOKEN), methods=['POST']) def respond(): # retrieve the message in JSON and then transform it to Telegram object update = telegram.Update.de_json(request.get_json(force=True), bot) chat_id = update.message.chat.id msg_id = update.message.message_id # Telegram understands UTF-8, so encode text for unicode compatibility text = update.message.text.encode('utf-8').decode() # for debugging purposes only print("got text message :", text) # the first time you chat with the bot AKA the welcoming message if text == "/start": # print the welcoming message bot_welcome = """ Welcome to coolAvatar bot, the bot is using the service from http://avatars.adorable.io/ to generate cool looking avatars based on the name you enter so please enter a name and the bot will reply with an avatar for your name. """ # send the welcoming message bot.sendMessage(chat_id=chat_id, text=bot_welcome, reply_to_message_id=msg_id) else: try: # clear the message we got from any non alphabets text = re.sub(r"\W", "_", text) # create the api link for the avatar based on http://avatars.adorable.io/ url = "https://api.adorable.io/avatars/285/{}.png".format(text.strip()) # reply with a photo to the name the user sent, # note that you can send photos by url and telegram will fetch it for you bot.sendPhoto(chat_id=chat_id, photo=url, reply_to_message_id=msg_id) except Exception: # if things went wrong bot.sendMessage(chat_id=chat_id, text="There was a problem in the name you used, please enter different name", reply_to_message_id=msg_id) return 'ok'
วิธีที่ใช้งานง่ายในการทำให้ฟังก์ชันนี้ทำงานคือ เราจะเรียกมันทุกวินาที เพื่อตรวจสอบว่ามีข้อความใหม่มาถึงหรือไม่ แต่เราจะไม่ทำอย่างนั้น แต่เราจะใช้ Webhook ซึ่งช่วยให้เราสามารถให้บอทโทรหาเซิร์ฟเวอร์ของเราทุกครั้งที่มีการเรียกข้อความ เพื่อที่เราจะได้ไม่ต้องทำให้เซิร์ฟเวอร์ของเราต้องทนทุกข์ในขณะที่รอข้อความมา
ดังนั้น เราจะสร้างฟังก์ชันที่เราจำเป็นต้องเรียกใช้เพื่อเปิดใช้งาน Webhook ของ Telegram โดยพื้นฐานแล้วจะบอก Telegram ให้โทรไปที่ลิงก์เฉพาะเมื่อมีข้อความใหม่เข้ามา เราจะเรียกฟังก์ชันนี้เพียงครั้งเดียวเท่านั้น เมื่อเราสร้างบอทในครั้งแรก หากคุณเปลี่ยนลิงก์ของแอป คุณจะต้องเรียกใช้ฟังก์ชันนี้อีกครั้งด้วยลิงก์ใหม่ที่คุณมี
เส้นทางที่นี่สามารถเป็นอะไรก็ได้ คุณเป็นคนที่จะเรียกมันว่า:
@app.route('/setwebhook', methods=['GET', 'POST']) def set_webhook(): # we use the bot object to link the bot to our app which live # in the link provided by URL s = bot.setWebhook('{URL}{HOOK}'.format(URL=URL, HOOK=TOKEN)) # something to let us know things work if s: return "webhook setup ok" else: return "webhook setup failed"
เมื่อทุกอย่างพร้อมแล้ว เรามาสร้างโฮมเพจสุดเก๋กันเพื่อที่เราจะได้รู้ว่าเครื่องยนต์ทำงานเสร็จแล้ว
@app.route('/') def index(): return '.' if __name__ == '__main__': # note the threaded arg which allow # your app to have more than one thread app.run(threaded=True)
มาดูเวอร์ชันเต็มของ app.py:
import re from flask import Flask, request import telegram from telebot.credentials import bot_token, bot_user_name,URL global bot global TOKEN TOKEN = bot_token bot = telegram.Bot(token=TOKEN) app = Flask(__name__) @app.route('/{}'.format(TOKEN), methods=['POST']) def respond(): # retrieve the message in JSON and then transform it to Telegram object update = telegram.Update.de_json(request.get_json(force=True), bot) chat_id = update.message.chat.id msg_id = update.message.message_id # Telegram understands UTF-8, so encode text for unicode compatibility text = update.message.text.encode('utf-8').decode() # for debugging purposes only print("got text message :", text) # the first time you chat with the bot AKA the welcoming message if text == "/start": # print the welcoming message bot_welcome = """ Welcome to coolAvatar bot, the bot is using the service from http://avatars.adorable.io/ to generate cool looking avatars based on the name you enter so please enter a name and the bot will reply with an avatar for your name. """ # send the welcoming message bot.sendMessage(chat_id=chat_id, text=bot_welcome, reply_to_message_id=msg_id) else: try: # clear the message we got from any non alphabets text = re.sub(r"\W", "_", text) # create the api link for the avatar based on http://avatars.adorable.io/ url = "https://api.adorable.io/avatars/285/{}.png".format(text.strip()) # reply with a photo to the name the user sent, # note that you can send photos by url and telegram will fetch it for you bot.sendPhoto(chat_id=chat_id, photo=url, reply_to_message_id=msg_id) except Exception: # if things went wrong bot.sendMessage(chat_id=chat_id, text="There was a problem in the name you used, please enter different name", reply_to_message_id=msg_id) return 'ok' @app.route('/set_webhook', methods=['GET', 'POST']) def set_webhook(): s = bot.setWebhook('{URL}{HOOK}'.format(URL=URL, HOOK=TOKEN)) if s: return "webhook setup ok" else: return "webhook setup failed" @app.route('/') def index(): return '.' if __name__ == '__main__': app.run(threaded=True)
นั่นคือโค้ดส่วนสุดท้ายที่คุณจะเขียนในบทช่วยสอนของเรา ตอนนี้ เราสามารถก้าวไปสู่ขั้นตอนสุดท้ายได้ โดยเปิดตัวแอพของเราบน Heroku

เปิดแอพของเราบน Heroku
เราต้องการบางสิ่งก่อนที่เราจะสร้างแอปของเรา
Heroku ไม่สามารถทราบได้ว่าโครงการของคุณใช้ไลบรารีใด ดังนั้นเราจึงต้องแจ้งโดยใช้ไฟล์ requirements.txt
ปัญหาทั่วไปคือคุณสะกดข้อกำหนดผิด ดังนั้นโปรดใช้ความระมัดระวังในการสร้างไฟล์ข้อกำหนดโดยใช้ pip:
pip freeze > requirements.txt
ตอนนี้คุณมีไฟล์ความต้องการของคุณพร้อมแล้ว
ตอนนี้คุณต้องการ Procfile
ซึ่งบอก Heroku ว่าแอปของเราเริ่มต้นที่ใด ดังนั้นให้สร้างไฟล์ Procfile
และเพิ่มสิ่งต่อไปนี้:
web: gunicorn app:app
ขั้นตอนการตีกลับ: คุณสามารถเพิ่มไฟล์ .gitignore
ในโครงการของคุณ เพื่อไม่ให้ไฟล์ที่ไม่ใช้แล้วถูกอัปโหลดไปยังที่เก็บ
จากแดชบอร์ด Heroku ของคุณ ให้สร้างแอปใหม่ เมื่อดำเนินการแล้ว ระบบจะนำคุณไปยังหน้า ปรับใช้ จากนั้นเปิดแท็บ การตั้งค่า ในหน้าต่างใหม่และคัดลอกโดเมนของแอปซึ่งจะมีลักษณะเช่น https://appname.herokuapp.com/
และวางลงในตัวแปร URL ภายใน credentials.py
ตอนนี้ กลับไปที่แท็บ Deploy และทำตามขั้นตอนต่อไปนี้:
หมายเหตุ: ผู้ใช้ Windows และ macOS สามารถทำตามขั้นตอนที่อธิบายไว้ที่นี่
เข้าสู่ระบบ Heroku:
$ heroku login
โปรดทราบว่าวิธีนี้บางครั้งอาจค้างอยู่ใน waiting for login
หากสิ่งนี้เกิดขึ้นกับคุณ ให้ลองเข้าสู่ระบบโดยใช้:
$ heroku login -i
เริ่มต้นที่เก็บ Git ในไดเร็กทอรีของเรา:
$ git init $ heroku git:remote -a {heroku-project-name}
ปรับใช้แอพ:
$ git add . $ git commit -m "first commit" $ git push heroku master
ณ จุดนี้ คุณจะเห็นความคืบหน้าของอาคารในอาคารผู้โดยสารของคุณ หากทุกอย่างเป็นไปด้วยดี คุณจะเห็นสิ่งนี้:
remote: -----> Launching... remote: Released v6 remote: https://project-name.herokuapp.com/ deployed to Heroku remote: remote: Verifying deploy... done.
ไปที่หน้าแอป (ลิงก์ของโดเมนที่คุณเคยคัดลอกมาก่อน) และเพิ่มที่ส่วนท้ายของลิงก์ /setwebhook
เพื่อให้ที่อยู่มีลักษณะดังนี้ https://appname.herokuapp.com/setwebhook
หากคุณเห็น webhook setup ok
แสดงว่าคุณพร้อมแล้ว!
ไปคุยกับบอทของคุณเลย
สัมผัสสุดท้าย เคล็ดลับ และลูกเล่น
ตอนนี้คุณมีบอทโทรเลขและทำงานตลอด 24 ชั่วโมงทุกวันโดยไม่จำเป็นต้องมีการแทรกแซงจากคุณ คุณสามารถเพิ่มตรรกะใดๆ ที่คุณต้องการให้กับบอทได้ ตัวอย่างเช่น คุณสามารถทำให้บอทของคุณมีความสมจริงมากขึ้นโดยการเพิ่มสถานะ "กำลังพิมพ์" และส่งสถานะรูปภาพดังนี้:
ข้อมูลโค้ดถัดไปจากฟังก์ชัน respond()
if text == "/start": # print the welcoming message bot_welcome = """ Welcome to coolAvatar bot, the bot is using the service from http://avatars.adorable.io/ to generate cool looking avatars based on the name you enter so please enter a name and the bot will reply with an avatar for your name. """ # send the welcoming message bot.sendChatAction(chat_id=chat_id, action="typing") sleep(1.5) bot.sendMessage(chat_id=chat_id, text=bot_welcome, reply_to_message_id=msg_id) else: try: # clear the message we got from any non alphabets text = re.sub(r"\W", "_", text) # create the api link for the avatar based on http://avatars.adorable.io/ url = "https://api.adorable.io/avatars/285/{}.png".format(text.strip()) # reply with a photo to the name the user sent, # note that you can send photos by url and telegram will fetch it for you bot.sendChatAction(chat_id=chat_id, action="upload_photo") sleep(2) bot.sendPhoto(chat_id=chat_id, photo=url, reply_to_message_id=msg_id) except Exception: # if things went wrong bot.sendMessage(chat_id=chat_id, text="There was a problem in the name you used, please enter different name", reply_to_message_id=msg_id)
ดังที่คุณเห็นในข้อมูลโค้ด เราได้เพิ่มการดำเนินการพิมพ์เมื่อเรากำลังจะส่งข้อมูลเกี่ยวกับบอทที่อยู่ในรูปแบบข้อความ และเพิ่มการดำเนินการรูปภาพที่อัปโหลดเมื่อเรากำลังจะส่งรูปภาพเพื่อทำให้บอทมีความสมจริงมากขึ้น . สามารถดูการดำเนินการเพิ่มเติมได้ที่นี่
คุณยังสามารถเปลี่ยนรูปภาพและคำอธิบายของบอทจากช่อง BotFather เพื่อให้เป็นมิตรมากขึ้น
ตัวอย่างง่ายๆ ของบอทโทรเลขมีอยู่ในหน้า python-telegram-bot บน GitHub
คุณสามารถสร้างจากบอทของเราและทำให้เป็นบอทสุดยอด AI ตัวต่อไปได้ ทั้งหมดที่คุณต้องทำคือรวมตรรกะของคุณในฟังก์ชัน respond()
ตัวอย่างเช่น ตรรกะของคุณสามารถอยู่ในโมดูลที่แยกจากกัน และสามารถเรียกภายในฟังก์ชัน respond()
ได้ดังนี้:
. ├── app.py ├── telebot │ ├── credentials.py │ ├──ai.py │ | . │ | you can build your engine here │ | . │ └── __init__.py └── botenv
และภายใน ai .py :
def generate_smart_reply(text): # here we can do all our work return "this is a smart reply from the ai!"
นำเข้าทันทีใน แอป .py :
import re from time import sleep from flask import Flask, request import telegram From telebot.ai import generate_smart_reply from telebot.credentials import bot_token, bot_user_name,URL
จากนั้นเพียงแค่เรียกมันภายในรหัส respond()
def respond(): # retrieve the message in JSON and then transform it to Telegram object update = telegram.Update.de_json(request.get_json(force=True), bot) chat_id = update.message.chat.id msg_id = update.message.message_id # Telegram understands UTF-8, so encode text for unicode compatibility text = update.message.text.encode('utf-8').decode() # for debugging purposes only print("got text message :", text) # here call your smart reply message reply = generate_smart_reply(text) bot.sendMessage(chat_id=chat_id, text=reply, reply_to_message_id=msg_id)
ตอนนี้คุณสามารถให้บอทของคุณทำงานในแบบที่คุณต้องการ ไปข้างหน้าและสร้างสิ่งที่ยิ่งใหญ่ต่อไป!
ฉันหวังว่าคุณจะสนุกกับการสร้างบอทโทรเลขตัวแรกของคุณ
แหล่งข้อมูลเพิ่มเติม
- การสร้าง Chatbot โดยใช้ Telegram และ Python
- การตั้งค่า Telegram Bot WebHook ด้วยวิธีง่าย ๆ
- Python-telegram-bot Repository
- การปรับใช้ Git บน Heroku
- เอกสาร Python Telegram Bot