Skip to content

Instantly share code, notes, and snippets.

@hgouveia
Last active December 10, 2022 17:07
Show Gist options
  • Save hgouveia/ac9d37dcb26bd3640d7ab47377197b33 to your computer and use it in GitHub Desktop.
Save hgouveia/ac9d37dcb26bd3640d7ab47377197b33 to your computer and use it in GitHub Desktop.
GW2 Trading Post Telegram Notifier
<?php
$GW2_API_KEY = ""; // Get it from https://account.arena.net/applications with 'account', 'tradingpost' scopes
$GW2_API_LANG = "en"; // en, es, de, fr and zh: https://wiki.guildwars2.com/wiki/API:2#Localisation
$TELEGRAM_API_KEY = ""; // Get this from telegram using @BotFather
$TELEGRAM_CHAT_ID = ""; // Search how to get the chatId of your username or use @RawDataBot
$SKIP_IF_ONLINE = true; // if the player is online, wont notify
// Get previous time of check
$CURRENT_TIME = time(); // UTC Time
$LAST_TIME_FILE = "last_check.log";
$FILE_CONTENT = @file_get_contents($LAST_TIME_FILE);
$TIME_DATA = explode(':', $FILE_CONTENT);
$LAST_TIME = count($TIME_DATA) > 0 && !empty($TIME_DATA[0]) ? intval($TIME_DATA[0]) : $CURRENT_TIME;
$ACCOUNT_AGE = count($TIME_DATA) > 1 ? intval($TIME_DATA[1]) : -1;
// Get Account info,
// if the age increase in every request, it means the player is online
if ($SKIP_IF_ONLINE) {
// https://wiki.guildwars2.com/wiki/API:2/account
$account = GW2API("/account", $GW2_API_KEY);
if ($ACCOUNT_AGE !== -1 && $account->age !== $ACCOUNT_AGE) {
_log("player is online, skipped [{$account->age}|$ACCOUNT_AGE]");
file_put_contents($LAST_TIME_FILE, "$CURRENT_TIME:$account->age");
return;
}
$ACCOUNT_AGE = $account->age;
}
// Get History sells
// https://wiki.guildwars2.com/wiki/API:2/commerce/transactions
$solds = [];
$history = GW2API("/commerce/transactions/history/sells", $GW2_API_KEY);
if ($history) {
foreach ($history as $item) {
// Group all the sold items that were purchased
// after the last check time
$item_time = strtotime($item->purchased); // fortunally this time is on UTC
if ($item_time >= $LAST_TIME) {
$solds[$item->item_id] = $item;
}
}
}
// Update last time of check
file_put_contents($LAST_TIME_FILE, "$CURRENT_TIME:$ACCOUNT_AGE");
// Get extended items info (name, icon)
if (count($solds) > 0) {
$item_ids = array_reduce($solds, function ($acc, $item) {
$acc[] = $item->item_id;
return $acc;
}, []);
// https://wiki.guildwars2.com/wiki/API:2/items
$items_info = GW2API("/items?lang=$GW2_API_LANG&ids=" . implode(",", $item_ids));
// Build message
$message = "";
foreach ($items_info as $item_extended) {
$item = $solds[$item_extended->id];
$price = priceToGold($item->price);
$message .= "\nItem: $item_extended->name";
$message .= "\nPrice: $price";
$message .= "\nQty: $item->quantity";
$message .= "\n$item_extended->icon";
$message .= "\n-----------";
}
// Send Telegram message
if ($message) {
// telegram maximum message length is 4096
// you could split in chunks and send in multiple messages
$message = substr("Solds:" . $message, 0, 4096);
sendMessage($TELEGRAM_API_KEY, $TELEGRAM_CHAT_ID, $message);
}
}
//-----------------------------------------------[Functions]
function GW2API($endpoint, $api_key = '')
{
$headers = $api_key ? ["Authorization: Bearer $api_key"] : [];
$url = "https://api.guildwars2.com/v2" . $endpoint;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array_merge(['Content-Type: application/json'], $headers));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
try {
$response = curl_exec($ch);
curl_close($ch);
return json_decode($response);
} catch (Exception $e) {
_log("ERROR: {$e->message}");
}
return null;
}
function sendMessage($api_key, $chat_id, $text)
{
$url = "https://api.telegram.org/bot" . $api_key . "/sendMessage";
$post_data = array(
"chat_id" => $chat_id,
"text" => $text
);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($post_data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
_log("TELEGRAM: " . var_export($response, true));
}
function priceToGold($price)
{
$price_formated = str_pad($price, 6, "0", STR_PAD_LEFT);
$len = strlen($price_formated);
// format as currency format ex: 1.000
$gold = number_format(intval(substr($price_formated, 0, $len - 4)), 0, ',', '.');
$silver = substr($price_formated, $len - 4, 2);
$copper = substr($price_formated, $len - 2);
return "$gold $silver $copper";
}
function _log($msg)
{
$log = "[" . date("Y-m-d h:i:s A") . "] $msg\n";
file_put_contents("debug.log", $log, FILE_APPEND);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment