Chatbots มักถูกขนานนามว่าเป็นการปฏิวัติวิธีที่ผู้ใช้โต้ตอบกับเทคโนโลยีและธุรกิจ พวกเขามีอินเทอร์เฟซที่ค่อนข้างเรียบง่ายเมื่อเทียบกับแอพทั่วไปเนื่องจากพวกเขาต้องการให้ผู้ใช้แชทเท่านั้นและแชทบอทควรเข้าใจและทำสิ่งที่ผู้ใช้เรียกร้องจากพวกเขาอย่างน้อยก็ในทางทฤษฎี
หลายอุตสาหกรรมเปลี่ยนการบริการลูกค้าไปสู่ระบบแชทบอท นั่นเป็นเพราะต้นทุนที่ลดลงอย่างมากเมื่อเทียบกับมนุษย์จริงๆและยังเป็นเพราะความทนทานและความพร้อมใช้งานที่คงที่ Chatbots ให้การสนับสนุนผู้ใช้ในระดับหนึ่งโดยไม่มีค่าใช้จ่ายเพิ่มเติมมากนัก
ปัจจุบันแชทบอทถูกนำมาใช้ในหลายสถานการณ์ตั้งแต่งานที่ต้องเผชิญเช่นการแสดงข้อมูลเวลาและสภาพอากาศไปจนถึงการดำเนินการที่ซับซ้อนมากขึ้นเช่นการวินิจฉัยทางการแพทย์เบื้องต้นและการสื่อสาร / การสนับสนุนลูกค้า คุณสามารถประดิษฐ์แชทบอทที่จะช่วยลูกค้าของคุณเมื่อพวกเขาถามคำถามบางอย่างเกี่ยวกับผลิตภัณฑ์ของคุณหรือคุณสามารถสร้างแชทบ็อตส่วนตัวที่สามารถจัดการงานพื้นฐานและเตือนคุณเมื่อถึงเวลาที่ต้องไปประชุมหรือโรงยิม
apache spark ทำอะไร
มีตัวเลือกมากมายในการใช้งานแชทบอทของคุณและหนึ่งในการใช้งานที่พบบ่อยที่สุดคือแพลตฟอร์มโซเชียลมีเดียเนื่องจากคนส่วนใหญ่ใช้เป็นประจำ เช่นเดียวกันกับแอพส่งข้อความโต้ตอบแบบทันทีแม้ว่าจะมีข้อแม้บางประการ
โทรเลข เป็นหนึ่งในแพลตฟอร์ม IM ที่ได้รับความนิยมมากขึ้นในปัจจุบันเนื่องจากช่วยให้คุณสามารถจัดเก็บข้อความบนคลาวด์แทนที่จะเป็นเพียงอุปกรณ์ของคุณและมีการสนับสนุนหลายแพลตฟอร์มที่ดีเนื่องจากคุณสามารถมี Telegram บน Android, iOS, Windows และอื่น ๆ แพลตฟอร์มอื่น ๆ ที่สามารถรองรับเวอร์ชันเว็บได้ การสร้าง chatbot บน Telegram นั้นค่อนข้างง่ายและต้องใช้เวลาเพียงไม่กี่ขั้นตอนซึ่งใช้เวลาน้อยมากในการดำเนินการให้เสร็จสิ้น Chatbot สามารถรวมเข้ากับกลุ่มและช่องทางโทรเลขและยังทำงานได้ด้วยตัวเอง
ในบทช่วยสอนนี้เราจะสร้างบ็อตโทรเลขที่ให้ภาพอวาตาร์แก่คุณ อวตารที่น่ารัก . ตัวอย่างของเราจะเกี่ยวข้องกับการสร้างบอทโดยใช้ Flask และปรับใช้บนเซิร์ฟเวอร์ Heroku ฟรี
ในการทำบทช่วยสอนนี้ให้เสร็จสมบูรณ์คุณจะต้อง Python 3 ติดตั้งในระบบของคุณเช่นเดียวกับ ทักษะการเขียนโปรแกรม Python . นอกจากนี้ความเข้าใจที่ดีเกี่ยวกับการทำงานของแอปจะเป็นส่วนเสริมที่ดี แต่ไม่ใช่สิ่งที่จำเป็นเนื่องจากเราจะอธิบายรายละเอียดส่วนใหญ่ที่เรานำเสนอ คุณยังต้องการ ไป ติดตั้งในระบบของคุณ
แน่นอนว่าบทช่วยสอนนี้ต้องใช้บัญชี Telegram ซึ่งฟรี คุณสามารถลงทะเบียน ที่นี่ . จำเป็นต้องมีบัญชี Heroku ด้วยและคุณสามารถรับได้ฟรี ที่นี่ .
ในการสร้าง chatbot บน Telegram คุณต้องติดต่อไฟล์ บอทพ่อ ซึ่งโดยพื้นฐานแล้วบอทที่ใช้ในการสร้างบอทอื่น ๆ
คำสั่งที่คุณต้องการคือ /newbot
ซึ่งนำไปสู่ขั้นตอนต่อไปนี้ในการสร้างบอทของคุณ:
บอทของคุณควรมีคุณสมบัติสองอย่างคือชื่อและชื่อผู้ใช้ ชื่อจะปรากฏขึ้นสำหรับบอทของคุณในขณะที่ชื่อผู้ใช้จะถูกใช้เพื่อพูดถึงและแบ่งปัน
หลังจากเลือกชื่อบอทและชื่อผู้ใช้ของคุณซึ่งต้องลงท้ายด้วย 'บอท' คุณจะได้รับข้อความที่มีโทเค็นการเข้าถึงของคุณและคุณจะต้องบันทึกโทเค็นการเข้าถึงและชื่อผู้ใช้ของคุณในภายหลังอย่างชัดเจนเนื่องจากคุณจะต้องใช้ในภายหลัง
เราจะใช้ Ubuntu ในบทช่วยสอนนี้ สำหรับผู้ใช้ Windows คำสั่งส่วนใหญ่ที่นี่จะใช้งานได้โดยไม่มีปัญหาใด ๆ แต่หากคุณประสบปัญหาใด ๆ กับการตั้งค่าสภาพแวดล้อมเสมือนโปรดปรึกษา ลิงค์นี้ . สำหรับผู้ใช้ Mac บทช่วยสอนนี้ควรใช้งานได้ดี
ขั้นแรกมาสร้างสภาพแวดล้อมเสมือน ช่วยแยกความต้องการของโครงการออกจากสภาพแวดล้อม Python ทั่วโลก
$ python -m venv botenv/
ตอนนี้เราจะมี botenv/
ไดเร็กทอรีซึ่งจะมีไลบรารี Python ทั้งหมดที่เราจะใช้ ไปข้างหน้าและเปิดใช้งาน virtualenv
โดยใช้คำสั่งต่อไปนี้:
$ source botenv/bin/activate
ไลบรารีที่เราต้องการสำหรับบอทของเราคือ:
วิธีรับหมายเลขบัตรเครดิตออนไลน์
คุณสามารถติดตั้งได้ในสภาพแวดล้อมเสมือนโดยใช้คำสั่ง 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 ที่โทรเลขจะเรียกเพื่อรับคำตอบสำหรับข้อความที่ส่งไปยังบอท
@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 โทรไปที่ลิงก์เฉพาะเมื่อมีข้อความใหม่มาถึง เราจะเรียกใช้ฟังก์ชันนี้เพียงครั้งเดียวเมื่อเราสร้างบอทครั้งแรก หากคุณเปลี่ยนลิงก์แอปคุณจะต้องเรียกใช้ฟังก์ชันนี้อีกครั้งด้วยลิงก์ใหม่ที่คุณมี
อุตสาหกรรมเครื่องสำอางมีมูลค่าเท่าไหร่ในปี 2017
เส้นทางที่นี่อาจเป็นอะไรก็ได้ คุณคือคนที่จะเรียกมันว่า:
@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 ไม่ทราบว่าโครงการของคุณใช้ไลบรารีใดเราจึงต้องบอกโดยใช้ requirements.txt
ไฟล์ - ปัญหาที่พบบ่อยคือคุณสะกดผิดข้อกำหนดดังนั้นโปรดระวัง - ในการสร้างไฟล์ข้อกำหนดโดยใช้ pip:
pip freeze > requirements.txt
ตอนนี้คุณมีไฟล์ข้อกำหนดของคุณพร้อมใช้งาน
ตอนนี้คุณต้องมี Procfile
ซึ่งจะบอก Heroku ว่าแอปของเราเริ่มต้นที่ใดดังนั้นให้สร้าง Procfile
ไฟล์และเพิ่มสิ่งต่อไปนี้:
web: gunicorn app:app
ขั้นตอนการตีกลับ: คุณสามารถเพิ่มไฟล์ .gitignore
ไฟล์ไปยังโครงการของคุณเพื่อไม่ให้ไฟล์ที่ไม่ใช้งานถูกอัปโหลดไปยังที่เก็บ
จาก Heroku ของคุณ แผงควบคุม สร้างแอปใหม่ เมื่อคุณทำมันจะนำคุณไปยังไฟล์ ปรับใช้ หน้า. จากนั้นเปิดไฟล์ การตั้งค่า ในหน้าต่างใหม่และคัดลอกโดเมนของแอปซึ่งจะเป็น https://appname.herokuapp.com/
และวางในตัวแปร URL ภายใน credentials.py
ตอนนี้กลับไปที่ไฟล์ ปรับใช้ แท็บและดำเนินการตามขั้นตอน:
บันทึก: ผู้ใช้ 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 เพื่อให้เป็นมิตรมากขึ้น
ตัวอย่างง่าย ๆ อีกมากมายของบ็อตโทรเลขสามารถพบได้ในไฟล์ หลามโทรเลขบอท หน้าบน 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)
ตอนนี้คุณสามารถให้บอททำงานในแบบที่คุณต้องการได้แล้วลงมือสร้างสิ่งที่ยิ่งใหญ่ต่อไป!
ฉันหวังว่าคุณจะสนุกกับการสร้าง Telegram bot ตัวแรกของคุณ
บ็อตโทรเลขเป็นซอฟต์แวร์อัตโนมัติที่สร้างขึ้นเพื่อช่วยให้ผู้ใช้ตอบสนองความต้องการโดยมีปฏิสัมพันธ์กับระบบน้อยที่สุดและให้ผลลัพธ์ที่ดีที่สุดโดยการทำความเข้าใจสิ่งที่ผู้ใช้พูด
บ็อตโทรเลขมีปัญหาบางอย่างในการพิจารณาใช้การเข้ารหัสจากต้นทางถึงปลายทางเนื่องจากใช้เทคนิคการเข้ารหัสที่แตกต่างจากที่ใช้ในข้อความของผู้ใช้ สิ่งนี้ทำให้บอทมีช่องโหว่และทำให้ผู้โจมตีสามารถเข้าถึงข้อความแชทได้ดังนั้นผู้ใช้ควรระมัดระวัง
Flask เป็นไมโครเว็บเฟรมเวิร์กที่เขียนด้วยภาษา Python เราใช้มันเพื่อสร้างเว็บเซิร์ฟเวอร์ของเราโดยใช้ความพยายามเพียงเล็กน้อย
Heroku เป็นแพลตฟอร์มระบบคลาวด์ as-a-Service (Paas) ที่รองรับภาษาโปรแกรมต่างๆ ในบทช่วยสอนนี้เราใช้มันเพื่อโฮสต์ตรรกะของเราและทำให้มันทำงานตลอดเวลา