Skip to Content
Menu
This question has been flagged
2 Replies
601 Views

Hello, I'm trying to open a contact's page on web browser from the controller via a method with the @http.route decorator I tried with the following statement:


'''
url="my URL"
request.redirect(url)
webbrowser.get('google-chrome').open(url)
'''

even trying to open just google chrome browser it doesn't work

os.system("PATH_chrome")

can you help me to solve this.

thank you.



Avatar
Discard
Author Best Answer

thank you for your reply.

What I'm trying to implement is a REST API using ODOO Controller. this API works with a VOIP solution application, as soon as I receive a call on the VOIP server, the server must send the notification to ODOO to retrieve some data on the contact who is calling, the API must provide this data and I must open the page of the contact who calls on the browser during the call. 

can you help me to solve this. 

thank you in advance for your help


Avatar
Discard

Here's an example of how you could implement a REST API using an ODOO controller to retrieve data on a contact when a call is received:

Copy code
from odoo import http
import json

class VoipApiController(http.Controller):
@http.route('/voip/call/<string:phone_number>', type='http', auth='none')
def handle_call(self, phone_number, **kwargs):
# retrieve contact data from the database
contact = http.request.env['res.partner'].search([('phone', '=', phone_number)], limit=1)
if contact:
# convert contact data to JSON format
contact_data = json.dumps(contact.read()[0])
return contact_data
else:
return "Contact not found"
In this example, the handle_call method is defined as the endpoint for the API using the @http.route decorator. The phone_number parameter is passed as a path variable in the URL. The method retrieves the contact data from the Odoo database using the http.request.env object, and the json.dumps() method is used to convert the data to a JSON format.

On the other hand, in the VOIP solution application, you could use the JavaScript XMLHttpRequest or fetch() method to send a request to the API endpoint and retrieve the data during a call.

Copy code
// using XMLHttpRequest
function getContactData(phoneNumber) {
var xhr = new XMLHttpRequest();
xhr.open('GET', '/voip/call/' + phoneNumber);
xhr.onreadystatechange = function() {
if (xhr.readyState === 4 && xhr.status === 200) {
var contactData = JSON.parse(xhr.responseText);
// do something with the contact data
}
};
xhr.send();
}

// using fetch
async function getContactData(phoneNumber) {
try {
const response = await fetch('/voip/call/' + phoneNumber);
const contactData = await response.json();
// do something with the contact data
} catch (error) {
console.error(error);
}
}
And you can use the window.open() method to open the contact page in the browser during the call.

Copy code
window.open("/web#id="+contact.id+"&view_type=form&model=res.partner");
Please note that this is a basic example and you may need to adapt it to your specific use case and add additional error handling and security measures as needed

Best Answer

you can use the redirect method of the http.Response object to redirect the user to a different URL. The redirect method takes the URL to redirect to as its only argument. For example:


Copy code

@http.route('/my_controller/redirect', type='http', auth='public')

def redirect_to_url(self):

    url = "http://www.example.com"

    return request.redirect(url)

Regarding opening the browser with a specific URL, it's not recommended to open it directly with the browser, it's better to redirect the user to the URL.


Also, the os.system("PATH_chrome") command is used to execute command line commands. In this case, it should be used to open the Chrome browser. you should use the full path of chrome.exe file in the system.


Copy code

import os

os.system('"C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe"')

Be aware that this will open a new chrome window, and the user will lose the context of the odoo application, so this is not recommended.

Avatar
Discard