Git Product home page Git Product logo

artifact2bot's Introduction

Artifact2Bot

Artifact2 Bot it’s a telegram bot completely written in Swift using Vapor and telegram-vapor-bot frameworks deployed via Heroku. Artifact2 works exactly as a Magic 8 ball, whenever you have a doubt you can ask the bot what to do or you can just ask to Flip a Coin. Layer_1

Hello I’m Artifact 2 Ask me any questions and I’ll help you make a decision. 🤖🤖🤖 Layer_2

Routes

Routing is the process of finding the appropriate request handler for an incoming request. By creating different handlers the bot is configured to reply to different commands or keywords. The code below shows how the default handler manage texts without command:

  • If the message contains the question mark, the bot will answer with a random reply
  • If the message contains Coinflip, the bot will randomly answer “Tail” or “Head”
  • If neither of the two the bot welcomes the user and explains the different keywords.
private static func defaultHandler(app: Vapor.Application, bot: TGBotPrtcl) {
        let handler = TGMessageHandler(filters: (.all && !.command.names(["/coinflip", "/show_buttons", "/info","/author"]))) { update, bot in
//            print("\(update.message!.text)")
            if update.message!.text!.contains("?"){
                let params: TGSendMessageParams = .init(chatId: .chat(update.message!.chat.id), text: "\(replies.randomElement()!)")
                try bot.sendMessage(params: params)
            }else if update.message!.text!.contains("Coinflip"){
                let params: TGSendMessageParams = .init(chatId: .chat(update.message!.chat.id), text: "\(coinFlip.randomElement()!)")
                try bot.sendMessage(params: params)
            }else{
                let params: TGSendMessageParams = .init(chatId: .chat(update.message!.chat.id), text: "Ask me a question (use question mark) or ask me to flip a coin with 'Coinflip' ")
                try bot.sendMessage(params: params)
            }
            
        }
        bot.connection.dispatcher.add(handler)
    }

Furthermore, the bot is triggered on certain commands, like:

  • /info
  • /author
  • /coinflip

This behavior is established in a different handler:

private static func commandCoinFlipHandler(app: Vapor.Application, bot: TGBotPrtcl) {
        let handler = TGCommandHandler(commands: ["/coinflip"]) { update, bot in
            try update.message?.reply(text: "\(coinFlip.randomElement()!)", bot: bot)
        }
        bot.connection.dispatcher.add(handler)
    }
    
    private static func commandInfoHandler(app: Vapor.Application, bot: TGBotPrtcl) {
        let handler = TGCommandHandler(commands: ["/info"]) { update, bot in
            try update.message?.reply(text: "Hello I'm Artifact 2 an experimental bot made for Server Side Swift branch, completly written in Swift using vapor and telegram-vapor-bot frameworks", bot: bot)
        }
        bot.connection.dispatcher.add(handler)
    }
    
    private static func commandAuthorHandler(app: Vapor.Application, bot: TGBotPrtcl) {
        let handler = TGCommandHandler(commands: ["/author"]) { update, bot in
            try update.message?.reply(text: "https://www.linkedin.com/in/edoardotroianiello/", bot: bot)
        }
        bot.connection.dispatcher.add(handler)
    }

Commands make it easier to understand the capabilities of a bot. If you register a set of commands at the botFather on telegram, on the chat view will appear a menu button with all the different commands that were registered, so a new user can easily figure out what he can do with the bot.

artifact2bot's People

Contributors

eddy10957 avatar

Stargazers

 avatar

Watchers

 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.