Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 2595

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 61k
Author
Asked: November 26, 20242024-11-26T06:52:08+00:00 2024-11-26T06:52:08+00:00

Integrating Telegram api with SvelteKit.

  • 61k

Read if:

  • You know how SvelteKit works.
  • You wanna get notified on telegram when someone submit a form on your SvelteKit website.

Introduction

I was working one of my projects and i wanted to get notified with the data as soon as user submit's the data.
At the time I thought of Email, WhatsApp, SMS and Telegram.
But I check my emails twice a day which is why i dropped it while for WhatsApp and SMS services you need to pay. So, I was left with telegram.
Now telegram provides a pretty good documentation of their api's.

So, first i tried some open sourced libraries which are available on NPM but none of them worked how i wanted to so i started to make my resource for that. I checked for SvelteKit with telegram, telegram for node but didn't workout for me.

So, i started working my own and made myself go through the telegram docs.

The only feature that i needed was sendMessage using bot api.
I found the api “https://api.telegram.org/bot${Token}/sendMessage?chat_id=${chatId}&text=${botMessage}“.

  • Token is your bot token that you can get from BotFather when you create a Bot on telegram.
  • chatId is your username's Id on telegram server, that you can get from RawDataBot.
  • botMessage is the message you need to send to bot and that will be seen in your chat.

Now lets get started with the integration part:

  • Create you SvelteKit App
  • In src/routes/index.svelte file make HTML Form.
<form on:submit|preventDefault={onSubmit} method="Post"> <label for="name">Name</label> <input type="text" id="name" name="name"/> <label for="email">Email</label> <input type="email" id="email" name="email"/> <label for="contact">Contact</label> <input type="tel" id="contact" name="contact"/> <button type="submit">Submit</button> </form> 
Enter fullscreen mode Exit fullscreen mode

  • Now add a onSubmit handler that we assigned to on:submit event.
<script> function onSubmit(e) { await fetch('/contact', { method: 'post', headers: {accept: 'application/json'}, body: new FormData(e.target as HTMLFormElement) }); } </script> 
Enter fullscreen mode Exit fullscreen mode

  • Create a new file in routes index.ts or index.js on your preference. index.ts is a shadow/page endpoint (click here for docs).
import type { RequestHandler } from '@sveltejs/kit';  export const post: RequestHandler = async ({ request }) => {     const form = await request.formData();     const name = String(form.get('name'));     const email = String(form.get('email'));     const contact = String(form.get('contact'));     const botMessage = `This is a test notification %0A Name: ${name} %0A Email: ${email} %0A Contact: ${contact}`; // %0A is url encoded '
' which is used for new line.     try {         const Token = import.meta.env.VITE_TELEGRAM_BOT_TOKEN;         const chatId = import.meta.env.VITE_TELEGRAM_CHAT_ID;         const url = `https://api.telegram.org/bot${Token}/sendMessage?chat_id=${chatId}&text=${botMessage}`;         const res = await fetch(url);         return await res.json();     } catch (err) {         console.error(err);         return null;     } }; 
Enter fullscreen mode Exit fullscreen mode

This method is needed to be done on server on server side because we wanna side our chatId and bot Token for security reasons. if we make a browser request this will leak in api call.

Thank you, this is my first post here if you find any errors please let me know i'll fix them.

This is me writing for you. If you wanna ask or suggest anything please put it in comment.

beginnerssveltetutorialwebdev
  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 1k
  • Popular
  • Answers
  • Author

    How to ensure that all the routes on my Symfony ...

    • 0 Answers
  • Author

    Insights into Forms in Flask

    • 0 Answers
  • Author

    Kick Start Your Next Project With Holo Theme

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.