Bungie-net / api

Resources for the Bungie.net API
Other
1.22k stars 92 forks source link

Telegram Bot with Bungie-Net/API #1379

Open BeppeTemp opened 3 years ago

BeppeTemp commented 3 years ago

Hello to all guardians.

I am a graduate student in cloud computing and I find myself having to develop a project with Azure for one of my exams.

Basically what I would like to create is a Telegram bot based on some services offered by the Azure platform (LUIS, Azure Bot Service, Function app and possibly a database) able to manage, through the use of Bungie-net / API, some aspects of the game world, the functions I would like to implement at the beginning are the following:

Daily notifications to know the inventory of specific vendors. For example, the gunsmith. I would also like to implement a sort of mechanism that allows you to check with a single glance if you already have a certain item (such as, for example, a mod sold by a gunsmith).

Ability to move weapons by communicating via natural language with the bot, for example "Move risk lover on hunter".

Ability to set up and equip builds, again using natural language.

Other functions to be defined ...

Since I am not very familiar with Bungie-net / API I would like to ask you if all this is possible within a Telegram bot and, if so, I would like to ask you how it is possible to manage authentication, for functions 2 and 3 , within a Telegram chat.

Thank you all.

floatingatoll commented 3 years ago

You'll need to run your bot on a hosted computing platform of some kind - for example, Heroku or AWS or Cloudflare Workers - where it can process and respond and create requests of its own accord. It will need to process both inbound callback HTTP requests from Bungie auth as well as create and maintain a Telegram platform connection to perform bot services.

I do not know how to construct these pieces in detail, but user authentication uses a standard OAuth2 callback workflow with both access and refresh tokens, that you'll be expected to refresh periodically using standard processes for doing so.

I hope that helps guide your investigations in a productive direction; I don't have experience assembling these pieces myself, so I can't offer technical specific guidance on reaching that outcome.

BeppeTemp commented 3 years ago

Thanks a lot for the answer. As I wrote as a hosting platform I intend to use Azure which already offers its services dedicated to BOT management and Language Understanding. My only doubt was about the authentication system that I didn't realize was standardized, but being so I'm sure I can find numerous examples of use online.

nandor-code commented 3 years ago

I just finished writing a Banshee-44 Mod email notified in AWS. Let me know if you need any specific info.

BeppeTemp commented 3 years ago

oh well, thanks a lot. Could you send me the piece of code relating to sending the HTTP request for receiving the gunsmith's inventory? in particular I would like to understand the parameters used.

nandor-code commented 3 years ago

oh well, thanks a lot. Could you send me the piece of code relating to sending the HTTP request for receiving the gunsmith's inventory? in particular I would like to understand the parameters used.

Sure I can do that. Keep in mind that Bungie's API is entirely over complicated and unnecessarily complex to use so please don't learn any anti-patterns from how they do this.

Here is the relevant code:

const getBanshee44 = (membershipId, membershipType, characterId, oAuth) => {
  return getVendor(
    membershipId,
    membershipType,
    characterId,
    "672118013", /* Banshee Vendor Hash */
    oAuth /* oAuth token from Bungie API */
  );
};
const getVendor = (
  membershipId,
  membershipType,
  characterId,
  vendorHash,
  oAuth
) => {
  let url = createEndpointURL(
    main_endpoints.rootPath + d2_endpoints.getVendor,
    {
      membershipType: membershipType,
      destinyMembershipId: membershipId,
      characterId: characterId,
      vendorHash: vendorHash,
    },
    {
      components: ["vendors", "vendorcategories", "vendorsales"],
      definitions: ["true"],
    }
  );

  return makeBungieAPIGet(url, oAuth);
};
const createEndpointURL = (url, pathParams, queryStrings) => {
  let output = url;

  Object.keys(pathParams).forEach((k) => {
    if (typeof pathParams[k] == "undefined" || pathParams[k] === "") {
      return;
    }

    let search = RegExp("{" + k + "}", "g");
    output = output.replace(search, pathParams[k]);
  });

  if (typeof queryStrings == "object")
    output += "?" + QueryString.stringify(queryStrings);

  return encodeURI(output);
}; 
const makeBungieAPIGet = (url, oAuth = false) => {
  let options = {
    headers: {
      "X-API-KEY": apiCreds.key,
      Authorization: oAuth
        ? "Bearer " + oAuth.access_token // Use the oAuth token to
        : "Basic " +
          new Buffer.from(
            apiCreds.clientId + ":" + apiCreds.clientSecret
          ).toString("base64"),
    },
  };

  //console.log(options);

  return axios.get(url, options);
};
BeppeTemp commented 3 years ago

thank you so much. do you have any advice or guide I could follow to use these APIs to the fullest? it is the first time that I use the API and I am realizing how these are actually very complex and the documentation in my opinion unclear and neglected.

nandor-code commented 3 years ago

thank you so much. do you have any advice or guide I could follow to use these APIs to the fullest? it is the first time that I use the API and I am realizing how these are actually very complex and the documentation in my opinion unclear and neglected.

Nope there are no really good guides I did a lot of experimenting with CURL to get the right api calls and then converted them into code.

BeppeTemp commented 3 years ago

One thing I can't understand is this: If I want to send this request:

/ Destiny2 / {membershipType} / Profile / {destinyMembershipId} / Character / {characterId} / Vendors / {vendorHash} /

I need 4 parameters: membershipType destinyMembershipId characterId vendorHash

I have read the documentation I cannot understand what the first three refer to, while the last is the hash code of the vendor (let's say Banshee) in your code you wrote the that the code is: 672118013, but from where the have you recovered? and how are the other parameters recovered?

andyschott commented 3 years ago

You get the membership ID back as part of the OAuth response. Once you have that, you can call /Destiny2/{membershipType}/Profile/{membershipId}/LinkedProfiles/ to get all of the characters on the account. If you only care about cross save accounts, I think the membershipType doesn't matter which value you pass here (but I'm not positive about that).

You can call /Destiny2/{membershipType}/Profile/{destinyMembershipId}/Character/{characterId}/Vendors/ to get all of the vendors available for a character. In general I don't think the vendor hashes change, but it would be best to not assume that, and call this endpoint before getting the data for a specific vendor.

BeppeTemp commented 3 years ago

ah ok, so to get inventory from a vendor I have to authenticate. Now everything is a bit clearer to me, the only obstacle is understanding how to authenticate, I have searched far and wide for an example, but unfortunately I find nothing clear. do you have any example? maybe written in python?

andyschott commented 3 years ago

I don't know Python, so I can't help there. There is this wiki page that might point you in the right direction.

If you search for python oauth that might help as well.