├── requirements.txt ├── rasa_flask_router.py ├── rasa-router └── index.php └── readme.md /requirements.txt: -------------------------------------------------------------------------------- 1 | Flask == 1.1.4 2 | requests == 2.25.1 3 | gunicorn 4 | -------------------------------------------------------------------------------- /rasa_flask_router.py: -------------------------------------------------------------------------------- 1 | import requests 2 | 3 | rasa_url = 'http://localhost:5005' 4 | chatwoot_url = 'http://localhost:3000' 5 | chatwoot_bot_token = '' 6 | 7 | 8 | def send_to_bot(sender, message): 9 | data = { 10 | 'sender': sender, 11 | 'message': message 12 | } 13 | headers = {"Content-Type": "application/json", 14 | "Accept": "application/json"} 15 | 16 | r = requests.post(f'{rasa_url}/webhooks/rest/webhook', 17 | json=data, headers=headers) 18 | return r.json()[0]['text'] 19 | 20 | 21 | def send_to_chatwoot(account, conversation, message): 22 | data = { 23 | 'content': message 24 | } 25 | url = f"{chatwoot_url}/api/v1/accounts/{account}/conversations/{conversation}/messages" 26 | headers = {"Content-Type": "application/json", 27 | "Accept": "application/json", 28 | "api_access_token": f"{chatwoot_bot_token}"} 29 | 30 | r = requests.post(url, 31 | json=data, headers=headers) 32 | return r.json() 33 | 34 | 35 | from flask import Flask, request 36 | app = Flask(__name__) 37 | 38 | 39 | @app.route('/rasa', methods=['POST']) 40 | def rasa(): 41 | data = request.get_json() 42 | message_type = data['message_type'] 43 | message = data['content'] 44 | conversation = data['conversation']['id'] 45 | contact = data['sender']['id'] 46 | account = data['account']['id'] 47 | 48 | if(message_type == "incoming"): 49 | bot_response = send_to_bot(contact, message) 50 | create_message = send_to_chatwoot( 51 | account, conversation, bot_response) 52 | return create_message 53 | 54 | if __name__ == '__main__': 55 | app.run(debug=1) 56 | # print(send_to_chatwoot(2,12,'3')) 57 | -------------------------------------------------------------------------------- /rasa-router/index.php: -------------------------------------------------------------------------------- 1 | '; 8 | 9 | 10 | $json = file_get_contents('php://input'); 11 | error_log("request payload: #{$json}", 0); 12 | 13 | $data = json_decode($json); 14 | 15 | $message_type = $data->message_type; 16 | $message = $data->content; 17 | $conversation = $data->conversation->id; 18 | $contact = $data->sender->id; 19 | $account = $data->account->id; 20 | 21 | 22 | error_log("message_type: {$message_type}", 0); 23 | error_log("message: {$message}", 0); 24 | error_log("conversation: {$conversation}", 0); 25 | error_log("contact: {$contact}", 0); 26 | error_log("account: {$account}", 0); 27 | 28 | if($message_type == "incoming") 29 | { 30 | error_log("sending message to bot: {$message}", 0); 31 | $bot_response = send_to_bot($contact, $message); 32 | error_log("bot replied: {$bot_response->text}", 0); 33 | $create_message = send_to_chatwoot($account, $conversation, $bot_response->text); 34 | } 35 | 36 | 37 | function send_to_bot($sender, $message){ 38 | global $rasa_url; 39 | $url = "{$rasa_url}/webhooks/rest/webhook"; 40 | $data = array('sender' => $sender, 'message' => $message); 41 | 42 | $options = array( 43 | 'http' => array( 44 | 'method' => 'POST', 45 | 'content' => json_encode( $data ), 46 | 'header'=> "Content-Type: application/json\r\n" . 47 | "Accept: application/json\r\n" 48 | ) 49 | ); 50 | 51 | $context = stream_context_create( $options ); 52 | $result = file_get_contents( $url, false, $context ); 53 | $response = json_decode($result); 54 | return $response[0]; 55 | } 56 | 57 | 58 | 59 | function send_to_chatwoot($account, $conversation, $message){ 60 | global $chatwoot_url, $chatwoot_bot_token; 61 | $url = "{$chatwoot_url}/api/v1/accounts/{$account}/conversations/{$conversation}/messages"; 62 | $data = array('content' => $message); 63 | 64 | $options = array( 65 | 'http' => array( 66 | 'method' => 'POST', 67 | 'content' => json_encode( $data ), 68 | 'header'=> "Content-Type: application/json\r\n" . 69 | "Accept: application/json\r\n" . 70 | "api_access_token: {$chatwoot_bot_token}" 71 | ) 72 | ); 73 | 74 | $context = stream_context_create( $options ); 75 | $result = file_get_contents( $url, false, $context ); 76 | error_log("chatwoot response: {$result}",0); 77 | $response = json_decode($result); 78 | return $result; 79 | } 80 | 81 | ?> 82 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # Archiving the Repo 2 | 3 | Moving this to [Chatwoot Implementation Samples](https://github.com/chatwoot/implementation-examples). 4 | 5 | ----- 6 | 7 | 8 | 9 | # Chatwoot Agent Bot Demo using Rasa 10 | 11 | > You should be running this demo on a local installation of Chatwoot and a non dockerised setup for the `localhost` and `ports` to be accessible for all the services involved. If you are intending to run in a remote server, ensure you change the localhost urls with appropriate IP addresses and make sure the ports should be accessible for all the services involded. 12 | 13 | This is a sample implementation of agent bot capabilities in chatwoot using [rasa](https://rasa.com/) . Rasa Open Source is a machine learning framework to automate text- and voice-based assistants. 14 | 15 | You can refer the [rasa documentation](https://rasa.com/docs/rasa/user-guide/installation/) to get it up and running in your machine. 16 | 17 | This implementation isn't a recommended set up for production, but just to illustrate the capabilities of the platform. Please build on top of this ideas discussed to have in running in production. 18 | 19 | 20 | 21 | Follow the given steps to get your agent bot integration up and running. 22 | 23 | **Refer the [Video Walkthrough](https://youtube.com/watch?v=KE4nKgepO_k) and [blog post](https://www.chatwoot.com/blog/its-a-bot-story)** 24 | 25 | 26 | ## Get a rasa project up and running. 27 | 28 | Go to a new directory and create a rasa project. If you have rasa installed in your machine you can get it up and running by follow in commands. Refer [docs](https://rasa.com/docs/rasa/user-guide/rasa-tutorial/) to get the installation up and running. 29 | 30 | ``` 31 | mkdir rasa 32 | cd rasa 33 | rasa init --no-prompt 34 | ``` 35 | 36 | go to `credentials.yml` file in the directory and ensure the following value is set. This is to ensure we can communicate with rasa through rest api 37 | 38 | ``` 39 | rest: 40 | # you don't need to provide anything here - this channel doesn't 41 | # require any credentials 42 | ``` 43 | 44 | start the rasa server with following command 45 | 46 | ``` 47 | rasa run -m models --enable-api --log-file out.log 48 | ``` 49 | 50 | ## Get your chatwoot up and create an agent bot 51 | 52 | go to your chatwoot directory and ensure your local server is running. Start a rails console in your directory. 53 | 54 | ``` 55 | bundle exec rails c 56 | ``` 57 | 58 | Inside the rails console, type the following commands to create an agent bot and get its access token. Save the retrieved token as you would need it in further step. 59 | 60 | ``` 61 | bot = AgentBot.create!(name: "Rasa Bot", outgoing_url: "http://localhost:8000") 62 | bot.access_token.token 63 | ``` 64 | 65 | Connect Agent Bot to your inbox by running the following command 66 | 67 | ``` 68 | AgentBotInbox.create!(inbox: Inbox.first, agent_bot: bot) 69 | ``` 70 | 71 | ## Clone this repo into your machine and run the rasa router script. 72 | 73 | 74 | clone repo using the following command. 75 | 76 | ``` 77 | git clone git@github.com:chatwoot/rasa-agent-bot-demo.git 78 | ``` 79 | ## Using Python 80 | open up the python file in your editor and change the follow values with appropriate ones. 81 | 82 | rasa_url, chatwoot_url and chatwoot_bot_token. 83 | 84 | Then run `pip install -r requirements.txt` and `python3 -m gunicorn --workers=1 test:app -b 0.0.0.0 ` 85 | 86 | ## Using PHP: 87 | open up the `rasa-router/index.php` file in your editor and change the follow values with appropriate ones. 88 | 89 | ``` 90 | $rasa_url = 'http://localhost:5005'; 91 | $chatwoot_url = 'http://localhost:3000'; 92 | $chatwoot_bot_token = ''; 93 | ``` 94 | 95 | run the php server in the rasa-router directory 96 | 97 | ``` 98 | cd rasa-router 99 | php -S localhost:8000 100 | ``` 101 | 102 | ## Connect to your chatwoot webwidget and start a conversation. 103 | 104 | if you are on your local machine, you can access the widget through the test page 105 | 106 | ``` 107 | http://localhost:3000/widget_tests 108 | ``` 109 | 110 | ## Notes 111 | 112 | You can also refer to the [RasaHQ / rasa-demo](https://github.com/RasaHQ/rasa-demo) for adding additional capabilities to your bot. If training rasa through scripts isn’t your thing, check the exciting rasa projects which gives a UI to create your rasa stories. 113 | - [Botfront](https://github.com/botfront/botfront) 114 | - [Articulate](https://github.com/samtecspg/articulate) 115 | 116 | You can build on top of the ideas discussed here to implement your solutions. Refer to the [chatwoot api](https://www.chatwoot.com/developers/api/) to see the available options in chatwoot for your bots.Pretty excited to see what you guys come up with. 117 | --------------------------------------------------------------------------------