how to attach library or javascript (External) in twig file?

In your theme.libraries.yml file add below lines
  Example:
  core/themes/bartik/bartik.libraries.yml:

  messages:
    css:
      theme:
        css/components/messages.css: {}  

  //In your twig file
  core/themes/bartik/templates/status-messages.html.twig:

  {# In a Twig template we attach the library. #}
  {{ attach_library('bartik/messages') }}

  Clear cache & it works

  external:
    js:
      'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js' : { external: true, defer: true, async: true }

  {{ attach_library('bartik/external') }}

Tags