Coder Social home page Coder Social logo

telega's Introduction

Inline Telegram Bot API library

Work in progress, there is an application for testing design of this library: https://github.com/iokasimov/elections-bot

Introduction

This library provides ORM-like toolkit to deal with methods of Telegram Bot API - types and classes were designed in terms of Objects and their Properties.

Objects description:

  • Update: object that webhook consumes, can be either an incoming Message, Callback query or Moving in/out some group chat.

  • Message: every object contains Origin and Content, can be either Direct, Forwarded or Replied.

  • Content: can be either Command, Text, File, Poll, Contact, Location or Venue.

  • File: Can be either Audio, Document, Photo, Video or Voice.

  • Origin: indicates where is message from, contains Sender.

  • Callback: this is what webhook consumes on pressing inline keyboard's button.

  • Group: can be either Basic or Super, contains chat ID and title.

  • Moving: indicates which Sender leaved some chat or which Senders joined, also contains Group.

  • Sender: can be either a Bot or a Human.

  • Member: shows the status of a bot or user in group chat, contains Sender.

Properties description:

  • Accessible: provides lens for objects within other objects.

  • Identifiable: provides identification of objects to use it for requests.

  • Persistable: provides persist methods that returns you value of some type on success. The "on success" definition is rather vague, for example: if you try to edit object and it remains the same, nothing will returns. Use persist_ to not decode API answer (because if decoding fails the next applicative or monadic action will not happen).

Available persistent actions over objects:

  • Forward: only Message.
  • Reply: everything that you can Send.
  • Send: Text, Keyboard, Audio, Document, Video, Voice, Photo, Location, Live Location, Poll.
  • Delete: Message.
  • Edit: Text, Keyboard, Live Location.
  • Stop: Live Location, Poll.

See payload and returning value in Persistable instances.

Also, you can Forward, Reply and Send objects Silently.

Also, there are special persistent actions for chat members:

  • Kick: kick user from group, user can not return unless unbanned first
  • Restrict: change what some member in group can do.
  • Promote: change what some admin in group can do.

Simple examples

Sending inline keyboard

let text = "Just click any button..."
let button1 = Button "Press me!" $ Callback "1"
let button2 = Button "Or press me!" $ Callback "2"
let button3 = Button "No, press me!" $ Callback "3"
let keyboard = Inline [button1, button2, button3]
void . persist . Send chat_id $ text :&: keyboard

Handling pressing buttons with notification

webhook (Query _ (Datatext cbq_id _ _ bn)) = do
	let text = "The button you pressed is: " <> bn
	persist $ Trigger @Notification cbq_id text

Deleting all incoming messages

webhook (Incoming _ msg) = do
	let chat_id = ident $ access @Origin msg
	persist (Delete @Message chat_id $ ident msg)

telega's People

Contributors

iokasimov avatar

Stargazers

方泓睿 avatar Washington Santos avatar  avatar

Watchers

 avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.