Advertisement

Rust from Beginner to Beginner

), and added API access functionality.

First, I forked the initial code of Flows.network to my GitHub account. You can view it at the following link: https://github.com/flows-network/telegram-gpt/tree/less-chatty

Then, I added the API URL in the Configuration option within Settings.


Next, I started modifying the code:

First, I added the http_req_wasi dependency in the [dependencies] section of the Cargo.toml file:

http_req_wasi = "0.10.2"




Then I made the following changes in the lib.rs file:

  1. I added the following use statements:
use serde_json::{Value,json};
use http_req::{
request::{Method, Request},
uri::Uri,
};




  1. I defined the url and fetched its value from Flows.network's Configuration:
    let api_url = std::env::var("api_url").unwrap();




  1. Parsed the json file to obtain the corresponding data:
else if text.eq_ignore_ascii_case("/top") || text.eq_ignore_ascii_case("/top@GameFiDash_bot") {
let fetch_uri: Uri = Uri::try_from(api_url).unwrap();
let mut bytes = Vec::new();
Request::new(&fetch_uri)
.method(Method::GET)
.send(&mut bytes).unwrap();
let json_str = String::from_utf8(bytes).unwrap();
log::info!("Received from web service : {}", json_str);
let c: Value = serde_json::from_str(&json_str).unwrap();
let data_arr = c["data"]["data"].as_array().unwrap();
let mut final_str = String::new();
final_str.push_str("Here are top 10 tokens from GamefiDash API:\n\n");
for d in data_arr {
let mut resp_str = String::new();
resp_str.push_str("Name: ");
resp_str.push_str(d["name"].as_str().unwrap());
resp_str.push_str("\n");
resp_str.push_str("Address: ");
resp_str.push_str(d["main_token_address"].as_str().unwrap());
resp_str.push_str("\n");
let token_arr = d["tokens"].as_array().unwrap();
for t in token_arr {
if let Some(p) = t["price"].as_f64() {
resp_str.push_str("Price: ");
resp_str.push_str(&p.to_string());
resp_str.push_str("\n\n");
break;
}
}
final_str.push_str(&resp_str);
}
final_str.push_str(&help_mesg);
_ = tele.send_message(chat_id, &final_str);
}



After completing the modifications, I performed a commit and push. My Telegram bot now has the ability to access an API to retrieve a list of Top Web3 Games, which is an improvement from yesterday.

Every timeafter git pushFlows.network automatically builds it, and the entire process is very smooth.



If there are errors during compilation, it also provides some guidance for resolution.


Dr. Yuan commented, "This is why many developers like using GitOps."