As a Facebook developer “Turn Facebook messenger into a horoscope lookup bot
- Tram Ho
Introduce
In a society where houses, people, from small children to the elderly, from rural to urban, they use social networks. Especially the thing that most people use is facebook. So what is facebook or how to play facebook, I will not mention it in this article, hey! With me as a programmer today, here I will guide you to play Facebook in a cool, real way.
My product made today is: Turn your personal messenger on facebook into a versatile bot.
Demo
Begin
I think it’s a Python code guy so in this article, I would like to use Python to dance ax through the eyes of workers.
- Preparation:
Những thứ chuẩn bị ở đây anh/em code python chắc chắn biết cài rồi nên mình chỉ liệt kê mà không hưóng dẫn nữa nhé
- Python 3.6
- Installing fbchat libraries and requests:
1 | $ pip install fbchat requests |
- Create a
folder project
: Sure, when the code we need to throw code into a folder here, I name the foldernguyenmanh_messenger_bot
. - Create
main.py
file: * This is the file I will code the main program in here =))nói những thứ thừa thãi v**
- Create file
tu_vi.py
: Used to code things related to the horoscope API - Create
session.json
file: Use to save the facebook session - Create file
nguyen_manh_bot.py
: Used to code bot messenger
- Create a
- List of APIs used in the article:
- Horoscope of the zodiac daily:
https://api.kma-chatbot.com/cunghoangdao.php?cung=<Tên cung hoàng đạo>
- Example zodiac: Pisces
- Horoscope 12 animals daily:
https://api.kma-chatbot.com/tuvi.php?tuoi=<con giáp>
- Armor example: Ox
- Horoscope of the zodiac daily:
Deploy code
- Code
tu_vi.py
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | import requests class TuVi (): def __init __ (self): pass def con_giap (self, Cgiap = ''): url = 'https://api.kma-chatbot.com/tuvi.php?tuoi= {}' .format (Cgiap.strip ()) loi_phan = '' # Judgment try: r = requests.get (url) data_json = r.json () set_attributes = data_json.get ('set_attributes') if set_attributes: loi_phan = '----- {} ----- n- {} n- {} n- {} n- {} n'.format ( set_attributes.get ('tvcongiap'), set_attributes.get ('congviec'), set_attributes.get ('tinhcam'), set_attributes.get ('taivan'), set_attributes.get ('cantrong') ) except: loi_phan = 'Maybe your armor is wrong :(' return loi_phan def cung_hoang_dao (self, cung_hd = ''): # This is for API and then you write yourself =)) Lazy! pass |
- Code
nguyen_manh_bot.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | # - * - coding: UTF-8 - * - from fbchat.models import * from fbchat import log, Client from tu_vi import TuVi class NguyenManhBot (Client): def onMessage (self, author_id, message_object, thread_id, thread_type, ** kwargs): self.markAsDelivered (thread_id, message_object.uid) self.markAsRead (thread_id) log.info ("{} from {} in {}". format (message_object, thread_id, thread_type.name)) if author_id! = self.uid: if message_object.text: if message_object.text == '/ Getid' or message_object.text == '/ getid': self.send (Message (text = message_object.author), thread_id = thread_id, thread_type = thread_type) elif '/ tuvi' in message_object.text: tuoi = message_object.text [message_object.text.index ('/ tuvi') + len ('/ tuvi'):] tuvi = TuVi () loi_phan = tuvi.con_giap (Cgiap = tuoi) self.send (Message (text = loi_phan), thread_id = thread_id, thread_type = thread_type) else: self.send (Message ( text = 'n I am Nguyen Manh. n - I will rep after going back to work - If I look at the horoscope, type / tuvi <age>; For example: n- Your message: {0} '. format ( message_object.text)), thread_id = thread_id, thread_type = thread_type ) |
- Code
main.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | import json from nguyen_manh_bot import NguyenanhBot def main (): # Load pre-login session if available với mở ('session.json') as f: session_cookies = json.load (f) client = NguyenManhBot ('username', 'password', session_cookies = session_cookies) # Get the session and save it to the file so that the next time you can log in session_cookies_new = client.getSession () with open ('session.json', 'w') as outfile: json.dump (session_cookies_new, outfile) # Listen to feedback from the messenger client.listen () if __name__ == '__main__': main () |
Put code on heroku
Now you have a pretty ok bot, you can run it locally, but to be more professional, we should put it on heroku so that it doesn’t have to turn on it to listen forever =))
- Step 1: Go to heroku to create an account
- Go to https://signup.heroku.com/login to register for an account
- Step 2: Create app heroku
- After registering an account and https://dashboard.heroku.com/apps to create an app
- Step 3: Install git and heroku on the device
- Heroku does use git so we can install git on the machine
- Window, download: https://git-scm.com/downloads
- Linux runs the command:
sudo apt-get install git
- Set heroku
- Window: https://devcenter.heroku.com/articles/heroku-cli
- Linux:
sudo snap install --classic heroku
- Heroku does use git so we can install git on the machine
- Step 4: Deploy code on heroku
- In order for heroku to run your code, you need to create a ‘Procfile’ file in the project with the content
1 | worker: python test.py |
Follow the steps below to put the code up
- Login to heroku
1 | $ heroku login |
- Initialize git
1 | $ git init |
- Add remote of heroku to git Thay
nguyen-manh-messenger
with the app name you created on heroku
1 | $ heroku git: remote -a nguyen-manh-messenger |
- Your Deploy app
1 2 3 | $ git add. $ git commit -am "First time doing that" $ git push heroku master |
Result
- Source code: https://github.com/nguyenmanh1997/nguyenmanh-messager
- After following the instructions above, you already have a
bot tra cứu tử vi
on your personal messenger. Now enjoy the fruits. - You rely on your code and upgrade more new functions.
- Good luck!