Skip to content

Custom Function

Selain fitur Search dan Ongkir, Malika Tools juga nyediain fitur Custom Function, di mana kamu bisa nulis kode Python kamu sendiri untuk ngeakomodir use case yang gak bisa diselesein dari fitur2 yang dah ada, tapi secara lebih fleksibel dan skalabel.

Prepare the Code

Sebelum dive deep ngecoba fitur ini, jangan lupa siapin kode kamu ya. Pastikan gak ada error di kode kamu sebelum dibuat webhook khusus untuk itu. Kamu bisa IDE apapun yang kamu mau—Jupyter Notebook, Google Colab, atau bahkan VS Code juga boleh yang penting kodenya Python.

Setelah fungsi kamu siap, kamu hanya perlu fungsi tunggal def tanpa import apa-apa, karena import sudah diatur di bagian Available Modules.

Available Modules

Terdapat beberapa modul yang bisa digunakan di dalam kode kamu. Ada beberapa modul yang built-in dari Python, dan ada modul yang hanya tersedia di Malika Tools ini.

Python Built-in Modules

Tidak semua module disediakan untuk runtime Custom Function ini ya, tapi kamu bisa akses beberapa additional module dari third party seperti:

No.ModuleDokumentasi
1numpyDokumentasi numpy
2mathDokumentasi math
3base64Dokumentasi base64
4pytzDokumentasi pytz
5collectionsDokumentasi collections
6evaluateDokumentasi eval
7requestDokumentasi requests.request
8reDokumentasi re
9datetimeDokumentasi datetime
10dtDokumentasi datetime.datetime
11timedeltaDokumentasi datetime.timedelta
12httpDokumentasi http

Malika Tools Modules

Selain itu, ada juga module-module internal milik Malika Tools yang bisa kamu pake. Kamu bisa melakukan pemanggilan modul-modul menggunakan fungsi call(<nama-modul>, <payload>) tersebut dengan cara seperti ini:

# Contoh fungsi untuk search profile Google Sheet
async def contoh():
payload = {
"profile_id": "qhomemart_12345",
"body": {
"query": "selang",
"limit": 10
}
}
# Berupa object dengan key 'message' dan 'result'
response = await call("search", payload)
return response['result']

Lalu, apa saja nama-nama modul beserta payload yang tersedia? Dapat dilihat pada tabel berikut:

Nama Modul
Cara Menggunakan ModulIsi variabel response

search

Melakukan pemanggilan search untuk mencari data di Database: Google Sheet, Jubelio, atau REST API. Perlu membuat profile baru dulu lalu dapetin ID-nya.

payload = {
"profile_id": "indo_super_group",
"body": {
"query": "selang",
"limit": 10,
"fallback_fts": False, # Optional, default: True
"fallback_limit": 5, # Optional, default: 10
"sort_by": "col-1", # Optional, default: first column
"sort_order": "asc", # Optional, default: "asc"
"connector": "gsheet" # Optional, default: "gsheet"
}
}
response = await call("search", payload)
{
"message": "<message>",
"result": [
{"col-1": "value", "col-2": "value2"},
{"col-1": "value3", "col-2": "value4"}
...
]
}
direct_search

Melakukan pemanggilan direct_search untuk mencari data di Google Sheet. Perlu membuat Google Sheet profile terlebih dahulu lalu dapetin ID-nya.

payload = {
"profile_id": "indo_super_group",
"body": {
"query": "selang",
"limit": 10,
"fallback_fts": False, # Optional
"fallback_limit": 10 # Optional
}
}
response = await call("direct_search", payload)
{
"message": "<message>",
"result": [
{"col-1": "value", "col-2": "value2"},
{"col-1": "value3", "col-2": "value4"}
...
]
}
cek_ongkir

Melakukan cek ongkir dengan API RajaOngkir

payload = {
"body": {
"origin": "Aksoro",
"destination": "Swalayan Negasa",
"courier": "jne",
"weight": 1300
}
}
response = await call("cek_ongkir", payload)
{
"message": "<message>",
"result": {
"origin": {
"province": "<province-1>",
"city": "<city-1>",
"subdistrict": "<subdistrict-1>"
},
"destination": {
"province": "<province-2>",
"city": "<city-2>",
"subdistrict": "<subdistrict-2>"
},
"ongkir": {
"code": "jne",
...
},
}
}
search_address

Melakukan pemanggilan search_address untuk mencari data alamat pakai query.

payload = {
"headers": {
"X-API-Key": "<api-key>" # api-key can be obtained from dev
},
"body": {
"query": "Aksoro"
}
}
response = await call("search_address", payload)
{
"message": "<message>",
"result": {
"id": "<id>",
"name": "<name>",
"address_name": "<address_name>",
"subdistrict": "<subdistrict>",
"district": "<district>",
"province": "<province>",
"country": "<country>",
"latitude": 0,
"longitude": 0,
"location_id": "<location-id>"
}
}
nearest_location

Mencari lokasi terdekat menggunakan fitur nearest location. Perlu membuat profil Nearest Location terlebih dahulu lalu dapetin ID-nya.

payload = {
"nearest_location_id": "ebliethos_12345",
"body": {
"query": "Aksoro" # Address to search
}
}
response = await call("nearest_location", payload)
{
"message": "<message>",
"result": [
{
"id": "<id-1>",
"name": "Cabang Cilacap",
"address_name": "Sampang, Cilacap Regency, Central Java, Indonesia",
...
"distance": 125.75644669718577
},
{
"id": "<id-2>",
"name": "Cabang Surabaya",
"address_name": "Rungkut, Surabaya, East Java, Indonesia",
...
"distance": 275.79143867984294
}
]
}
gsheet_add_rows

Menambahkan banyak data ke Google Sheet. Perlu membuat Google Sheet profile terlebih dahulu lalu dapetin ID-nya.

payload = {
"headers": {
"X-API-Key": "<api-key>" # api-key can be obtained from dev
},
"profile_id": "indo_super_group",
"exclude_id": True, # If true, the first column will be excluded from the data. Default: True
"body": [
{"col-1": "new-value-1", "col-2": "new-value-2"},
{"col-1": "new-value-3", "col-2": "new-value-4"}
...
]
}
response = await call("gsheet_add_rows", payload)
{
"message": "<message>",
"result": {
"id": "<profile-id>"
}
}

Example

Misal kamu ingin membuat fungsi untuk menghitung total harga produk yang rumit dan tidak bisa diselesein sama AI. Pastiin kamu udah nulis dan udah coba dites di lokal. Untuk contoh kodenya kurang lebih seperti ini:

def total(product_price: float, is_java: str, is_cod: str, delivery_cost: float):
final_price = 0
if is_java == "True":
final_price = product_price
else:
if(delivery_cost < 50000):
final_price = product_price + delivery_cost/2
else:
final_price = product_price + (delivery_cost - 25000)
if is_cod == "True":
final_price += final_price * 0.03
return final_price

More Complex Example

Misal kamu ingin membuat fungsi untuk menghitung ongkir dari kantor Aksoro, yang mana data asalnya didapat dari modul nearest location dari gudang Ebliethos, kamu dapat melakukan hal seperti ini:

async def nearest_ongkir():
# Define the destination first
destination = "Aksoro"
# Call the nearest_location module
payload_nearest = {
"nearest_location_id": "ebliethos_o77xhq8bsa2teolpf6ek",
"body": {
"query": destination
}
}
response = await call("nearest_location", payload_nearest)
# Get nearest address then pick the address name
nearest = response['result'][0]["address_name"]
# Then call the cek_ongkir module
payload_ongkir = {
"body": {
"origin": nearest, # Use nearest address
"destination": destination,
"courier": "jne", # or 'sicepat' or 'ninja' or anything else
"weight": 1000 # in KG
}
}
response = await call("cek_ongkir", payload_ongkir)
return response['result']

Fungsi tersebut akan mengembalikan hasil seperti ini:

{
"origin": {
"province": "Jawa Tengah",
"city": "Kabupaten Cilacap",
"subdistrict": "Sampang"
},
"destination": {
"province": "DI Yogyakarta",
"city": "Kabupaten Sleman",
"subdistrict": "Gamping"
},
"ongkir": {
"code": "jne",
"name": "Jalur Nugraha Ekakurir (JNE)",
"costs": [
{
"service": "JTR",
"description": "JNE Trucking",
"cost": [
{
"value": 40000,
"etd": "4-5",
"note": ""
}
]
},
{
"service": "REG",
"description": "Layanan Reguler",
"cost": [
{
"value": 12000,
"etd": "3-6",
"note": ""
}
]
},
{
"service": "YES",
"description": "Yakin Esok Sampai",
"cost": [
{
"value": 15000,
"etd": "1-1",
"note": ""
}
]
}
]
}
}

Mudah bukan? 😁👍

Create New Function

Setelah kodenya sudah siap dan tanpa error, kamu bisa membuat profil/webhook baru di Malika Tools sebagai berikut:

Membuat Profil/Webhook Baru untuk Custom Function

Membuat Profil/Webhook Baru untuk Custom Function

Register the Custom Function Webhook to Cekat AI

Kalau buat kodenya dah berhasil, kamu bisa daftarin profil/webhook baru itu ke Cekat AI lewat fitur Integrations-nya.

Alur Mendaftarkan Webhook ke Cekat AI

Alur Mendaftarkan Webhook ke Cekat AI

Sebelum di-prompting untuk menyesuaikan terhadap fungsi baru yang udah dibuat, tes dulu apakah fungsinya bekerja dengan baik apa belum seperti yang ada di gambar pada langkah 4 dan 5.

Update and Delete

Terdapat juga opearasi update dan delete fungsi yang sudah dibuat di bagian bawah kanan tampilan.

Update dan Delete Custom Function

Update dan Delete Custom Function

Run (New)

Sekarang, kamu bisa langsung menjalankan fungsi di dalam dashboard tools.malika.ai loh! Caranya dengan klik tombol Run di bagian tombol bawah kanan.

Menjalankan Custom Function

Menjalankan Custom Function