A Facebook messenger bot based on a finite state machine.
- Python 3.4
- Facebook Page and App
- HTTPS Server
pip3 install -r requirements.txt
FB_VERIFY_TOKEN
, FB_ACCESS_TOKEN
and WEATHER_KEY
MUST be set to environment variable.
-
Locally
Using
ngrok
as a proxy../ngrok http 1029
After that,
ngrok
would generate a https URL.Run the server
python3 app.py
-
Heroku
Deploy on Heroku
go_to : No condition go to next state
advance : Check condition and then go to next state
go_back : No condition go back to initial state
-
The initial state is set to
state_init
. -
Every time
state_init
state is triggered togo_to
tostart_state0
and the bot will ask user to do something. -
When user enter "天氣", the state wil change from
start_state0
toask_zone_state1
and the bot will ask user which zone in Tainan. -
When user enter "xx區" which is real zone in Tainan, the state will change from
ask_zone_state1
toask_interval_state2
and the bot will ask user to type "現在" or "一週". -
-
When user enter "現在", the state will change from
ask_interval_state2
torealtime_state3
and the bot will ask user to type "溫度", "降雨機率" or "天氣狀態". -
When user enter "一週", the state will change from
ask_interval_state2
tooneweek_state4
and replies one week weather, then the state will triggered togo_to
tofinish_state9
show "歡迎下次再來詢問天氣~" andgo_back
tostate_init
.
-
-
When user enter "溫度" "降雨機率" "天氣狀態", the state will change from
realtime_state3
toask_temp_state5
ask_rain_state6
ask_pheno_state7
separately and replies realtime temperature, then the state will triggrd togo_to
toask_oneweek_state8
ask the user "你還想知道未來一周的天氣嗎?". -
When user enter something is similar to yes such as "好", "想" and "yes", the state will change from
ask_oneweek_state8
tooneweek_state4
; otherwise, when user enter something is similar to no such as "不好", "不想" and "no", the state will change fromask_oneweek_state8
tofinish_state9
. -
When the state is
ask_zone_state1
ask_interval_state2
realtime_state3
ask_oneweek_state8
, the user can type "結束" togo_to
statefinish_state9
andgo_back
tostate_init
.
- state_init
- go_to: start_state0
- advance: ask_zone_state1
- advance: ask_interval_state2
- advance: realtime_state3
- advance: ask_temp_state5, ask_rain_state6, ask_pheno_state7
- go_to: ask_oneweek_state8
- advance: oneweek_state4
- advance: finish_state9
- go_back: state_init
- advance: finish_state9
- advance: finish_state9
- go_back: state_init
- advance: oneweek_state4
- go_to: ask_oneweek_state8
- advance: ask_temp_state5, ask_rain_state6, ask_pheno_state7
- advance: oneweek_state4
- advance: finish_state9
- go_back: state_init
- advance: finish_state9
- advance: finish_state9
- go_back: state_init
- advance: realtime_state3
- advance: finish_state9
- go_back: state_init
- advance: ask_interval_state2
- advance: ask_zone_state1
- go_to: start_state0